drupal8snip

كتابات: 

// 20190110
// Drupal 8 generate uuid.
$uuid = \Drupal::service('uuid')->generate();

// Checks whether a string appears to be in the format of a UUID.
use Drupal\Component\Uuid\Uuid;
Uuid::isValid($uuid_to_validate);
// drupal 8 load term by custom field
$term = current(\Drupal::entityTypeManager()->getStorage('taxonomy_term')
  ->loadByProperties(['field_CUSTOM_NAME' => 'THE_VALUE', 'vid' => 'VOCABULARY_MACHINE_NAME'])
);

// Load term by tid.
$term = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->load($tid);
// Drupal 8 fetch taxonomy term custom field from term object.
print $term->get('field_CUSTOM_NAME')->getValue()[0]['value'];
print $term->get('tid')->getValue()[0]['value'];
print $term->get('vid')->getValue()[0]['target_id'];
print term->get('name')->getValue()[0]['value'];
// Taxonomy term save multiple values.
$VALUES = $term->get('field_CUSTOM_NAME')->getValue();
$VALUES[]['value'] = 'NEW VALUE';
$term->field_CUSTOM_NAME->setValue($VALUES);
// Drupal 8 save and get load cookie.
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Cookie;
$SymfonyResponse = new Response();
$cookie = new Cookie('YOUR_COOKIE_KEY', 'VALUE_HERE_PLEASE', 0, '/' , NULL, FALSE);
$SymfonyResponse->headers->setCookie($cookie);
$SymfonyResponse->send();

// Load cookie.
$request_cookie = \Drupal::request()->cookies->all();
print $request_cookie['YOUR_COOKIE_KEY'];
// Drupal 8 get user IP.
$current_ip = \Drupal::request()->getClientIp();
// Drupal8 save node programmatically.
$node = Node::create([
  'type'        => 'article',
  'title'       => 'TITLE_HERE',
  'field_category' => [
    'target_id' => 'TID'
  ],
]);
$node->save();
// 20190115
// Drupal 8 read request headers for current user request and get header values.
use Symfony\Component\HttpFoundation\ServerBag;
$headers = new ServerBag($_SERVER);
$headers = $headers->getHeaders();
print_r($headers);
  // Drupal 8 load nodes by custom Taxonomy field.
  $nodes = \Drupal::entityTypeManager()
    ->getStorage('node')
    ->loadByProperties(['type' => 'CONTENT_TYPE', 'field_CUSTOM_TERM_REF_FIELD' => $tid]);
// Drupal8 load all nodes from a specific content type.
$nids = \Drupal::entityQuery('node')->condition('type','CONTENT_TYPE')->execute();
$nodes =  \Drupal\node\Entity\Node::loadMultiple($nids);

فضلاً إذا أعجبتك هذه الصفحة لاتنسى أن تقوم بمشاركتها