Save node terms

Submitted by root on Thu, 11/05/2020 - 17:08
Code
<?php $term_names = ['Bat', 'Bi']; $node_terms = []; foreach ($term_names as $name) { $tids = \Drupal::entityQuery('taxonomy_term')->condition('vid', 'vocabulary_id')->condition('name', $name)->execute(); if(!$tids) { $term = \Drupal\taxonomy\Entity\Term::create([ 'vid' => 'vocabulary_id', 'name' => $name, ]); $term->save(); $node_terms[] = ['target_id' => $term->id()]; } else { $node_terms[] = ['target_id' => array_shift($tids)]; } } $node->field_field_name->setValue($node_terms); ?>

Get translated taxonomy

Submitted by root on Thu, 11/05/2020 - 16:31
Code
<?php $terms = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadTree('vid', 0, NULL, TRUE); $current_langcode = \Drupal::languageManager()->getCurrentLanguage()->getId(); foreach ($terms as &$term) { if ($term->hasTranslation($current_langcode)) { $term = $term->getTranslation($current_langcode); } } ?>

Import node and translate

Submitted by root on Tue, 10/27/2020 - 12:13
Code
<?php $bundle = 'story'; $default_langcode = 'eu'; $translation_langcode = 'es'; $vid = 'tags'; $uid = 1; $taxonomy_terms = self::getAllTerms($vid); $author = \Drupal::entityTypeManager()->getStorage('user')->load($uid); $taxonomy_term = $sailkapen_terms[$tid] ?? NULL; // check if exists $nodes = \Drupal::entityTypeManager() ->getListBuilder('node') ->getStorage() ->loadByProperties([ 'field_[key]' => $key, ]); // create or load node if($nodes) { $node = array_shift($nodes); } else { $values = array( // 'nid' => $nid, 'langcode' => $default_langcode, 'type' => $bundle, 'uid' => $uid, 'status' => 1, 'promote' => 0, 'title' => $title, 'field_signatura' => $signatura, ); $node = \Drupal\node\Entity\Node::create($values); $node->save(); } // create or load translation if( !$node->hasTranslation($translation_langcode) ) { $node_tr = $node->addTranslation($translation_langcode); $node_tr->uid = $uid; } else { $node_tr = $node->getTranslation($translation_langcode); } $node_tr->body->format = 'basic_html'; $node_tr->body->value = $pp[2]; $node_tr->title = self::getTitleFromDescription($pp[2]); $node_tr->setOwnerId($author); $node_tr->save(); if(!empty($body_content)) { $node->body = array( 'summary' => NULL, 'value' => $body_content, 'format' => 'basic_html' // 'full_html'... ); } // set plain text value if( !empty($field_value) ) { $node->field_hasiera->setValue( $field_value ); } // set entity reference value if(!empty($sailkapen)) { $node->field_field_key->setValue(['target_id' => $tid]); } $node->field_raw->setValue($r); $node->save(); ?>

Drupal links

Submitted by root on Mon, 09/21/2020 - 06:52
Code
<?php // Link use Drupal\Core\Link; $link = Link::createFromRoute('This is a link', 'entity.node.canonical', ['node' => 1]); // Url use Drupal\Core\Link; use Drupal\Core\Url; $link = Link::fromTextAndUrl('This is a link', Url::fromRoute('entity.node.canonical', ['node' => 1])); $link = Link::fromTextAndUrl('This is a link', Url::fromUri('base:robots.txt')); // External links $link = Link::fromTextAndUrl('This is a link', Url::fromUri('http://www.google.com')); // links with only the fragment (without url) $link = Link::fromTextAndUrl('This is a link', Url::fromUri('internal:#fragment')); // Using the data provided by a user $link = Link::fromTextAndUrl('This is a link', Url::fromUserInput('/node/1'); // The param passed to fromUserInput must start with /,#,? or it will throw an exception. // Linking entities. $link = Link::fromTextAndUrl('This is a link', Url::fromUri('entity:node/1')); // Entities are a special case, and there are more ways to link them: $node = Node::load(1); $link = $node->toLink(); $link->setText('This is a link'); // And even using the route: $link = Link::fromTextAndUrl('This is a link', Url::fromRoute('entity.node.canonical', ['node' => 1])); // Drupal usually expects a render array if you are going to print the link, so the Link object has a method for that: $link->toRenderable(); ?>

PHP functions comment template

Submitted by root on Wed, 07/22/2020 - 08:09
Code
/** * Does something interesting * * @param Place $where Where something interesting takes place * @param integer $repeat How many times something interesting should happen * * @throws Some_Exception_Class If something interesting cannot happen * @author Monkey Coder <mcoder@facebook.com> * @return Status */

Local time

Submitted by root on Thu, 05/21/2020 - 06:45
Code
<?php setlocale(LC_TIME, 'eu_ES'); $unix_timestamp = time(); $titulu = strftime('%Yko %Bren %ea, %A', $unix_timestamp); ?>