Drupal 8 - Valid Email address

\Drupal::service('email.validator')->isValid($mail);

 


Drupal 8 Html::load()

/** @var string $html */
$html = <<<HTML
         <a href="/" title="home">Home</a>
HTML;
/** @var DOMElement $element */
foreach (Drupal\Component\Utility\Html::load($html)->getElementsByTagName('a') as $element) {
    $href = $element->getAttribute('href');
    $title = $element->getAttribute('title');
    echo "{$title} is redirecting to {$href}" . PHP_EOL;
}

 


Drupal 8: Re-run update N hook

UPDATE key_value SET value='i:8000;' WHERE collection = 'system.schema' AND name = 'module_name';

Or

\Drupal::keyValue('system.schema')->set('module_name', (int) 8000);

Even

drush ev "\Drupal::keyValue('system.schema')->set('module_name', (int) 8000)";

drush eval "drupal_set_installed_schema_version("module_name", 8000)"

 


Git set credential helper

git config credential.helper store

 


Drupal 8: Re-run cron while it is already running

# Execute `php-eval` drush's command to release cron's flag
drush php-eval "\Drupal::lock()->release('cron');"

 


Load all taxonomy vocabularies

\Drupal\taxonomy\Entity\Vocabulary::loadMultiple()

 


Load taxonomy tree based on vocabulary

\Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadTree($vid);

 


Pantheon - Drupal 8 + Upstream Updates

# Official Pantheon documentation reference here: https://pantheon.io/docs/core-updates/#overwrite-core
 git pull -Xtheirs git://github.com/pantheon-systems/drops-8.git master

 


Drupal 8 Load EntityViewDisplay instance


use Drupal\Core\Entity\Entity\EntityViewDisplay;

// Define values to load entity
$entity_type = 'node';
$bundle = 'article';
$view_mode = 'teaser';

// Setup entity view display ID
$entity_id = sprintf('%s.%s.%s', $entity_type, $bundle, $view_mode);

// Load entity view display object using entity id.
$entityViewDisplay = EntityViewDisplay::load($entity_id);

 


Drupal 8 countQuery()

$result = \Drupal::database()->select('node')
  ->condition('type', 'article')
  ->countQuery()
  ->execute()->fetchField();