entity_extra_field.module in Entity Extra Field 8
Same filename and directory in other branches
The hook implementation for the entity extra field module.
File
entity_extra_field.moduleView source
<?php
/**
* @file
* The hook implementation for the entity extra field module.
*/
use Drupal\Core\Entity\ContentEntityFormInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Url;
use Drupal\entity_extra_field\Entity\EntityExtraFieldInterface;
/**
* Implements hook_entity_extra_field_info().
*/
function entity_extra_field_entity_extra_field_info() {
$field_info = [];
foreach (entity_extra_field_storage()
->loadMultiple() as $extra_field) {
if (!$extra_field instanceof EntityExtraFieldInterface) {
continue;
}
$display_type = $extra_field
->getDisplayType();
if (!in_array($display_type, [
'view',
'form',
])) {
continue;
}
$display_type = $display_type === 'view' ? 'display' : $display_type;
$field_name = $extra_field
->name();
$entity_type_id = $extra_field
->getBaseEntityTypeId();
$entity_bundle_id = $extra_field
->getBaseBundleTypeId();
$field_info[$entity_type_id][$entity_bundle_id][$display_type][$field_name] = [
'label' => $extra_field
->label(),
'description' => $extra_field
->description(),
'weight' => 0,
];
}
return $field_info;
}
/**
* Implements hook_entity_view().
*/
function entity_extra_field_entity_view(array &$build, \Drupal\Core\Entity\EntityInterface $entity, \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display, $view_mode) {
entity_extra_field_display('view', $build, $entity, $display);
}
/**
* Implements hook_form_alter().
*/
function entity_extra_field_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
$form_object = $form_state
->getFormObject();
if (!$form_object instanceof ContentEntityFormInterface) {
return;
}
$entity = $form_object
->getEntity();
$display = $form_object
->getFormDisplay($form_state);
entity_extra_field_display('form', $form, $entity, $display);
}
/**
* Implements hook_theme().
*/
function entity_extra_field_theme($existing, $type, $theme, $path) {
return [
'entity_extra_field' => [
'render element' => 'element',
'path' => $path . '/templates',
'file' => 'entity_extra_field.theme',
'template' => 'entity-extra-field',
],
];
}
/**
* Entity extra field display.
*
* @param $type
* The display type, (e.g. view, form).
* @param $build
* An array of elements to attach the extra field.
* @param \Drupal\Core\Entity\EntityInterface $entity
* An entity instance.
* @param \Drupal\Core\Entity\Display\EntityDisplayInterface $display
* An entity display instance.
*
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
* @throws \Drupal\Component\Plugin\Exception\PluginException
*/
function entity_extra_field_display($type, &$build, \Drupal\Core\Entity\EntityInterface $entity, \Drupal\Core\Entity\Display\EntityDisplayInterface $display) {
$contexts = [];
$storage = entity_extra_field_storage();
$extra_field_ids = $storage
->getQuery()
->condition('display.type', $type)
->condition('base_bundle_type_id', $entity
->bundle())
->condition('base_entity_type_id', $entity
->getEntityTypeId())
->execute();
if (!empty($extra_field_ids)) {
$contexts[$entity
->getEntityTypeId()] = $entity;
$extra_fields = $storage
->loadMultiple(array_values($extra_field_ids));
/** @var \Drupal\entity_extra_field\Entity\EntityExtraField $extra_field */
foreach ($extra_fields as $extra_field) {
$contexts[$extra_field
->getEntityTypeId()] = $extra_field;
$all_conditions_pass = $extra_field
->getFieldTypeConditionsAllPass();
if (!$extra_field instanceof EntityExtraFieldInterface || !$extra_field
->hasDisplayComponent($display) || !$extra_field
->hasConditionsBeenMet($contexts, $all_conditions_pass)) {
continue;
}
$build[$extra_field
->name()] = $extra_field
->build($entity, $display);
if ($attachments = $extra_field
->getBuildAttachments()) {
$build[$extra_field
->name()]['#attached'] = $attachments;
}
$build[$extra_field
->name()]['#cache']['tags'] = [
'entity_extra_field',
"entity_extra_field:{$type}.{$entity->getEntityTypeId()}.{$entity->bundle()}",
];
}
}
}
/**
* Get entity extra field storage.
*
* @return \Drupal\Core\Entity\EntityStorageInterface
* The entity extra field storage.
*
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
*/
function entity_extra_field_storage() {
return \Drupal::entityTypeManager()
->getStorage('entity_extra_field');
}
/**
* Implements hook_theme_suggestions_HOOK().
*/
function entity_extra_field_theme_suggestions_entity_extra_field(array $variables) {
$original = $variables['theme_hook_original'];
$view_mode = $variables['element']['#view_mode'];
$field = $variables['element']['#field'];
$entity_type_id = $field
->getBaseEntityTypeId();
$bundle_id = $field
->getBaseBundleTypeId();
$field_name = $field
->name();
$base_suggestion = $original . '__' . $entity_type_id . '__' . $bundle_id;
$suggestions = [];
$suggestions[] = $base_suggestion;
$suggestions[] = $base_suggestion . '__' . $field_name;
$suggestions[] = $base_suggestion . '__' . $field_name . '__' . $view_mode;
return $suggestions;
}
Functions
Name | Description |
---|---|
entity_extra_field_display | Entity extra field display. |
entity_extra_field_entity_extra_field_info | Implements hook_entity_extra_field_info(). |
entity_extra_field_entity_view | Implements hook_entity_view(). |
entity_extra_field_form_alter | Implements hook_form_alter(). |
entity_extra_field_storage | Get entity extra field storage. |
entity_extra_field_theme | Implements hook_theme(). |
entity_extra_field_theme_suggestions_entity_extra_field | Implements hook_theme_suggestions_HOOK(). |