devel_wizard-2.x-dev/templates/spell/entity_type/content/module.php.twig
templates/spell/entity_type/content/module.php.twig
/**
* Implements hook_theme_suggestions_HOOK().
*
* @phpstan-param array{{ '<' }}string, mixed> $variables
*
* @phpstan-return array{{ '<' }}string>
*/
function {{ module.machineName }}_theme_suggestions_{{ content.id }}(array $variables): array {
$entityTypeId = $variables['elements']['#entity_type'];
/** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
$entity = $variables['elements']["#{$entityTypeId}"];
$view_mode = strtr($variables['elements']['#view_mode'], '.', '_');
$bundle = $entity->bundle();
$id = $entity->id();
return [
"{$entityTypeId}__{$view_mode}",
"{$entityTypeId}__{$bundle}",
"{$entityTypeId}__{$bundle}__{$view_mode}",
"{$entityTypeId}__{$id}",
"{$entityTypeId}__{$id}__{$view_mode}",
];
}
/**
* Prepares variables for {{ content.label }} templates.
*
* Default template: {{ content.id }}.html.twig.
*
* Most themes use their own copy of {{ content.id }}.html.twig. The default is located
* inside "/.../{{ module.machineName }}/templates/{{ module.machineName }}.{{ content.id }}.html.twig".
* Look in there for the full list of variables.
*
* @param array $variables
* An associative array containing:
* - elements: An array of elements to display in view mode.
* - {{ content.id }}: {{ content.label }} object.
* - url: The Url object.
*
* @phpstan-param array{{ '<' }}string, mixed> $variables
*
* @throws \Drupal\Core\Entity\EntityMalformedException
*/
function template_preprocess_{{ content.id }}(array &$variables): void {
$variables['view_mode'] = $variables['elements']['#view_mode'];
$variables['{{ content.id }}'] = $variables['elements']['#{{ content.id }}'];
/** @var \{{ content.interface_fqn }} ${{ content.id }} */
${{ content.id }} = $variables['{{ content.id }}'];
$variables['url'] = ${{ content.id }}->toUrl(
'canonical',
[
'language' => ${{ content.id }}->language(),
],
);
$variables['linkRelationshipType'] = {{ module.machineName }}_is_entity_page(${{ content.id }})
? 'canonical'
: NULL;
$variables += ['content' => []];
foreach (Element::children($variables['elements']) as $key) {
$variables['content'][$key] = $variables['elements'][$key];
}
$variables['attributes']['role'] = 'article';
}
/**
* Checks whether the current page is the full page view of the passed-in entity.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* An entity.
*
* @return bool
* Returns TRUE if the current page is the canonical page of the $entity.
*/
function {{ module.machineName }}_is_entity_page(EntityInterface $entity): bool {
$entityTypeId = $entity->getEntityTypeId();
$routeMatch = \Drupal::routeMatch();
$pageEntity = ($routeMatch->getRouteName() === "entity.$entityTypeId.canonical")
? $routeMatch->getParameter($entityTypeId)
: NULL;
return !empty($pageEntity) && $pageEntity->id() === $entity->id();
}
