Notes

Drupal 8 format date
\Drupal::service('date.formatter')->format($timestamp, $type, $format, $timezone, $langcode);
/* $type:  - One of the built-in formats: 
 *  'short', 'medium', 'long', 'html_datetime', 'html_date', 
 *  'html_time', 'html_yearless_date', 'html_week', 'html_month', 'html_year'. 
 */

 

Friday, September 1, 2017 - 22:32
Drupal 8 - Valid Email address
\Drupal::service('email.validator')->isValid($mail);

 

Sunday, August 20, 2017 - 13:32
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;
}

 

Thursday, July 27, 2017 - 10:22
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)"

 

Friday, March 31, 2017 - 09:26
Git set credential helper
git config credential.helper store

 

Thursday, March 23, 2017 - 16:42
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');"

 

Monday, February 20, 2017 - 07:18
Load all taxonomy vocabularies
\Drupal\taxonomy\Entity\Vocabulary::loadMultiple()

 

Tuesday, January 31, 2017 - 10:23
Load taxonomy tree based on vocabulary
\Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadTree($vid);

 

Monday, January 30, 2017 - 16:45
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

 

Saturday, January 14, 2017 - 20:27
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);

 

Monday, December 12, 2016 - 10:50