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);
// 20190308 Bali
// Loading current user details.
$user = User::load(\Drupal::currentUser()->id());
// 20190308
// Getting and load nodes related to a specific user using entityQuery.
  $query = \Drupal::entityQuery('node')
    ->condition('status', 1) //published or not
    ->condition('type', 'CONTENT_TYPE') //content type
    ->condition('uid', \Drupal::currentUser()->id()) //content type
    ->pager(10); //specify results to return
  $nids = $query->execute();

  // Using entityTypeManager.
  $nodes = \Drupal::entityTypeManager()
    ->getStorage('node')
    ->loadByProperties(['type' => 'CONTENT_TYPE', 'uid' => \Drupal::currentUser()->id()]);
// 20190308
// Load file URL from file field value.
  $fid = $entity->get('field_IMAGE')->getValue()[0]['target_id'];
  $file = \Drupal\file\Entity\File::load($fid);
  $image_url = $file->url();
  // $image_url = file_create_url($file->getFileUri());

// Getting styled URL from drupal 8 custom image field.
  $fid = $entity->get('field_IMAGE')->getValue()[0]['target_id'];
  $file = \Drupal\file\Entity\File::load($fid);
  $image_uri = $file->getFileUri();
  $styled_url = ImageStyle::load('large')->buildUrl($image_uri);
// 20190617
// Page cache can not be controlled by max-age or contexts for anyonymous. You can prevent that kind of caching by using this method to avoid caching for the page or prevent caching in forms.
// Source: https://drupal.stackexchange.com/a/219583/24113
\Drupal::service('page_cache_kill_switch')->trigger();
// 20190619
// Save user programmatically.
    $language = \Drupal::languageManager()->getCurrentLanguage()->getId();
    $new_user = User::create();
    // Mandatory.
    $new_user->setPassword('PASSWORD_AS_TEXT');
    $new_user->enforceIsNew();
    $new_user->setEmail('[email protected]');
    $new_user->setUsername('USER_NAME');
    // Optional.
    $new_user->set('init', '[email protected]');
    $new_user->set('langcode', $language);
    $new_user->set('preferred_langcode', $language);
    $new_user->set('preferred_admin_langcode', $language);
    $new_user->addRole('ROLE_NAME');
    // Set out custom values.
    $new_user->set('field_CUSTOM', 'VALUE');
    $new_user->activate();

    _user_mail_notify('register_no_approval_required', $new_user);
    user_login_finalize($new_user);
    \Drupal::messenger()->addStatus($this->t('Registration successful. You are now logged in.'));
    $form_state->setRedirect('');
// 20190619
// Load user by specific custom field.
$id_number_check = \Drupal::entityQuery('user')
      ->condition('status', 1)
      // ->condition('roles', 'ROLE_NAME')
      // ->condition('roles',['ROLE_NAME_1', 'ROLE_NAME_2'], 'IN')
      // ->condition('status', 1)
      // ->sort('uid', 'ASC')
      // ->range(0, 1)
      ->condition('field_NAME', 'VALUE')
      ->execute();

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