menu_entity_index.module in Menu Entity Index 8
Main Menu Entity Index module file.
File
menu_entity_index.moduleView source
<?php
/**
* @file
* Main Menu Entity Index module file.
*/
use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Form\FormStateInterface;
/**
* Implements hook_entity_extra_field_info().
*/
function menu_entity_index_entity_extra_field_info() {
$extra = [];
$entity_type_manager = \Drupal::service('entity_type.manager');
$tracker = \Drupal::service('menu_entity_index.tracker');
foreach ($tracker
->getTrackedEntityTypes() as $entity_type_id) {
$subtype_entity_type = $entity_type_manager
->getDefinition($entity_type_id)
->getBundleEntityType();
$subtypes = $entity_type_manager
->getStorage($subtype_entity_type)
->loadMultiple();
foreach ($subtypes as $machine_name => $subtype) {
$extra[$entity_type_id][$subtype
->id()]['form']['menu_entity_index'] = [
'label' => t('Menu Links'),
'description' => t('Lists all menu links that reference this entity.'),
'visible' => FALSE,
];
}
}
return $extra;
}
/**
* Implements hook_form_alter().
*/
function menu_entity_index_form_alter(&$form, FormStateInterface $form_state, $form_id) {
$form_object = $form_state
->getFormObject();
if (!$form_object || !$form_object instanceof EntityForm) {
// We only care about entity forms.
return;
}
elseif ($form_object
->getOperation() != 'edit') {
// New entities are always unreferenced.
return;
}
$tracker = \Drupal::service('menu_entity_index.tracker');
$entity = $form_state
->getFormObject()
->getEntity();
if (!in_array($entity
->getEntityTypeId(), $tracker
->getTrackedEntityTypes())) {
// Tracking is not enabled for this entity type.
return;
}
// Add reference tracker pseudo-field.
$data = $tracker
->getHostData($entity);
$form['menu_entity_index'] = [
'#type' => 'details',
'#title' => \Drupal::translation()
->formatPlural(count($data), 'Referenced by 1 menu link', 'Referenced by @count menu links'),
'#open' => FALSE,
'#access' => \Drupal::currentUser()
->hasPermission('view menu_entity_index form field'),
];
$form['menu_entity_index']['table'] = [
'#type' => 'table',
'#header' => [
t('Menu'),
t('Level'),
t('Label'),
t('Language'),
],
'#empty' => t('- None -'),
];
foreach ($data as $row) {
$form['menu_entity_index']['table'][] = [
'menu' => [
'#markup' => $row['menu_name'],
],
'level' => [
'#markup' => $row['level'],
],
'label' => empty($row['link']) ? [
'#markup' => $row['label'],
] : [
'#type' => 'link',
'#title' => $row['label'],
'#url' => $row['link'],
],
'language' => [
'#markup' => $row['language'],
],
];
}
}
/**
* Implements hook_entity_insert().
*/
function menu_entity_index_entity_insert($entity) {
\Drupal::service('menu_entity_index.tracker')
->updateEntity($entity);
}
/**
* Implements hook_entity_update().
*/
function menu_entity_index_entity_update($entity) {
\Drupal::service('menu_entity_index.tracker')
->updateEntity($entity);
}
/**
* Implements hook_entity_delete().
*/
function menu_entity_index_entity_delete($entity) {
\Drupal::service('menu_entity_index.tracker')
->deleteEntity($entity);
}
/**
* Implements hook_entity_type_alter().
*/
function menu_entity_index_entity_type_alter(array &$entity_types) {
// Set a views data handler on behalf of menu_link_content, if none exists.
$views_data = $entity_types['menu_link_content']
->getHandlerClass('views_data');
if (is_null($views_data)) {
$entity_types['menu_link_content']
->setHandlerClass('views_data', 'Drupal\\menu_entity_index\\ViewsData\\MenuLinkContent');
}
}
Functions
Name | Description |
---|---|
menu_entity_index_entity_delete | Implements hook_entity_delete(). |
menu_entity_index_entity_extra_field_info | Implements hook_entity_extra_field_info(). |
menu_entity_index_entity_insert | Implements hook_entity_insert(). |
menu_entity_index_entity_type_alter | Implements hook_entity_type_alter(). |
menu_entity_index_entity_update | Implements hook_entity_update(). |
menu_entity_index_form_alter | Implements hook_form_alter(). |