drupal


kint nodes entities taxonomies views links menus images composer blocks twig

kint

  • Comment out the following line in Kint.php to print available methods.
'Kint\\Parser\\ClassMethodsPlugin'
  • Add the following snippet to settings.php or settings.local.php and lower that value if still getting out of memory error
if (class_exists('Kint')) {
  \Kint::$max_depth = 4;
}

nodes

// get node info
if ($node = \Drupal::request()->attributes->get('node')) {
  $node_type = $node->bundle();
  $nid = $node->id();
  $node_title = $node->getTitle();
}

// get field valkue from node
$node = \Drupal::routeMatch()->getParameter('node');
if ($node instanceof \Drupal\node\NodeInterface) {
  $nid = $node->id();
  $my_field_value = $node->my_field->value;
}

// load node
use Drupal\node\Entity\Node;
$nid = 123;
$node = Node::load($nid)

entity queries

use Drupal\node\Entity\Node;
$query = \Drupal::entityQuery('node')
  // You can chain as many conditions as necessary.
  ->condition('type', 'page')
  ->condition('field_some_field', 14, '>')
  ->accessCheck(TRUE);
$results = $query->execute();
// $results is array of nids so you still need to load the nodes.
$nodes = Node::loadMultiple($results);

taxonomies

// get tid from term label
$properties['name'] = $name;
$properties['vid'] = $vid;
$terms = \Drupal::entityManager()->getStorage('taxonomy_term')->loadByProperties($properties);
$term = reset($terms);
$tid = $term->id();

// get term name from tid
use Drupal\taxonomy\Entity\Term;
use Drupal\taxonomy\TermInterface;
$term_name = Term::load($term_id)->get('name')->value;

views

See https://www.drupal.org/docs/theming-drupal/twig-in-drupal/twig-template-naming-conventions#s-views for more info on template naming

views-view--[viewid]--[view-display-id].html.twig
views-view--[viewid]--[view-display-type].html.twig
views-view--[view-display-type].html.twig
views-view--[viewid].html.twig
views-view.html.twig
// get nid from uri
use Drupal\Core\Url;
use Drupal\node\Entiy\Node;
$url = Url::fromUri($uri);
$node = Node::load($url->getRouteParameters()['node']);
$nid = $node->id();

// get url from uri
use Drupal\Core\Url;
$url = Url::fromUri($uri);
// create new menu item
$link = MenuLinkContent::create([
  'title'      => $menu_link_titles[$i],
  'link'       => ['uri' => 'internal:##'],
  'menu_name'  => 'main',
  'weight'     => $i,
  'expanded'   => TRUE,
]);

images

// get URL from mid

// apply image style in template file

// run markup through text format

composer

  • composer update updates everything to its most recent versions ignoring the versions in composer.lock
  • composer install installs all packages using the exact versions outlined in composer.lock
  • composer update --lock if the lock file is out of date

blocks

Necessary pieces for a custom block with custom variables, template, CSS/JS and stored configuration

  1. in /src/Form extend ConfigFormBase with custom class with getEditableConfigNames, getFormId, buildForm and submitForm methods
  2. in /src/Plugin/Block extend BlockBase with custom class using config load via \Drupal::config in a build method
  3. implement hook_theme() to pass variables in step 2 and the template name
  4. implement preprocess_page() to attach library for any CSS & JS files: $variables['#attached']['library'][] ="module-name/library-name"
  5. add routing if necessary in module-name.routing.yml with following syntax
module_name.form_settings:
  path: '/admin/desired-path'
  defaults:
    _form: '\Drupal\module_name\Form\formClassName'
    _title: ''
  requirements:
    _permission: 'access administration pages'
  options:
    _admin_route: TRUE

twig

// Print block easily or other entities
...

// Link function

// Create random number