A general list and cheatsheet of useful Drupal programming structures and "gotchas."
\Drupal::request()->query->all()
\Drupal::request()->query->get('paramKey')
\Drupal\user\Entity\User::load(**$id**)
\Drupal\user\Entity\User::create()
\Drupal::currentUser()
$user_lookup = \Drupal::entityQuery('user')
->condition('field_user_first_name', 'Foo');
$user_nids = $user_lookup->execute();
\Drupal::currentUser()->isAutheticated()
\Drupal::currentUser()->isAnnonymous()
$current_user_entity = User::load(\Drupal::currentUser()->id());
\Drupal::entityTypeManager()->getStorage('node')->load($nid);
\Drupal::routeMatch()->getParameter('node');
$nodes = \Drupal::entityTypeManager()
->getStorage('node')
->loadByProperties(array('field_machine_name' => $field_machine_name));
$referenced_entities = $node->get('field_entity_ref')
->referencedEntities();
$referencedEntity = $association
->get('field_entity_ref')
->first()
->get('entity')
->getTarget()
->getValue();
$ drush genc 0 —kill –types=article
\Drupal::entityManager()->getStorage('paragraph')->load($pid)
$multiParagraph1 = Paragraph::create([
'type' => 'paragraph_machine_name',
'field_machine_name' => 'Field value',
]);
$multiParagraph2 = Paragraph::create([
'type' => 'paragraph_machine_name',
'field_machine_name' => 'Field value',
]);
$multiParagraph1->save();
$multiParagraph2->save();
$node->paragraph_entity_ref_field[] = [
'target_id' => $multiParagraph1->id(),
'target_revision_id' => $multiParagraph1->getRevisionId(),
];
$node->paragraph_entity_ref_field[] = [
'target_id' => $multiParagraph2->id(),
'target_revision_id' => $multiParagraph2->getRevisionId(),
];
use Drupal\Core\Block\BlockBase;
class BlockClassName extends BlockBase {
public function build() {
// Code here...
return array(
'#type' => 'type',
'#markup' => 'markup'
);
}
}
return $this->redirect('<front>');
KernelEvents::CONTROLLER; // The CONTROLLER event occurs once a controller was found for handling a request.
KernelEvents::EXCEPTION; // The EXCEPTION event occurs when an uncaught exception appears.
KernelEvents::FINISH_REQUEST; //The FINISH_REQUEST event occurs when a response was generated for a request.
KernelEvents::REQUEST; // The REQUEST event occurs at the very beginning of request dispatching.
KernelEvents::RESPONSE; // The RESPONSE event occurs once a response was created for replying to a request.
KernelEvents::TERMINATE; // The TERMINATE event occurs once a response was sent.
KernelEvents::VIEW; // The VIEW event occurs when the return value of a controller is not a Response instance.
Accepts 1 argument, queue_name, and 1 option, --time-limit (in seconds)
drush -l environment queue-run queue_name --time-limit=seconds
Example:
drush -l local queue-run update_users --time-limit=600
$ drush cim -y --partial --source=modules/_custom/my_module/config/install/
alternatively,
Navigate to the devel tab on the admin site, then click the 'Execute PHP' tab.
$config_installer = \Drupal::service('config.installer');
$config_installer->installDefaultConfig('module', 'mymodule_name');
All views templates can be overridden with a variety of names, using the view, the display ID of the view, the display type of the view, or some combination thereof.
For each view, there will be a minimum of two templates used. The first is used for all views: views-view.html.twig.
The second template is determined by the style selected for the view. Note that certain aspects of the view can also change which style is used; for example, arguments which provide a summary view might change the style to one of the special summary styles.
The default style for all views is views-view-unformatted.html.twig.
Many styles will then farm out the actual display of each row to a row style; the default row style is views-view-fields.html.twig.
Here is an example of all the templates that will be tried in the following case:
View, named foobar. Style: unformatted. Row style: Fields. Display: Page.
views-view--foobar--page.html.twig
views-view--page.html.twig
views-view--foobar.html.twig
views-view.html.twig
views-view-unformatted--foobar--page.html.twig
views-view-unformatted--page.html.twig
views-view-unformatted--foobar.html.twig
views-view-unformatted.html.twig
views-view-fields--foobar--page.html.twig
views-view-fields--page.html.twig
views-view-fields--foobar.html.twig
views-view-fields.html.twig
Important! When adding a new template to your theme, be sure to flush the theme registry cache!
getValue()
returns an array.
->value
returns field value.
$entity->get('field_name')->getValue();
is synonymous with $entity->field_name->getValue();
.
$status = \Drupal::requestStack()->getCurrentRequest()->attributes->get('exception');
if ($status && $status->getStatusCode() == 404){
//...
}