ajax_dashboard-8.x-2.x-dev/modules/ajax_dashboard_entity_dashboard/ajax_dashboard_entity_dashboard.module
modules/ajax_dashboard_entity_dashboard/ajax_dashboard_entity_dashboard.module
<?php
/**
* @file
* AJAX Dashboard: Entity Dashboards module hooks.
*/
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Routing\RouteMatchInterface;
/**
* Implements hook_help().
*/
function ajax_dashboard_entity_dashboard_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
// Main module help for the ajax_dashboard module.
case 'help.page.ajax_dashboard_entity_dashboard':
$output = '';
$output .= '<h2>' . t('About') . '</h2>';
$output .= '<p>' . t('AJAX Dashboard: Entity Dashboards enables the ability to place a configurable dashboard on any content entity.') . '</p>';
$output .= '<h3>' . t('Usage') . '</h3>';
$output .= '<ol>';
$output .= '<li>' . t('Go to /admin/config/system/ajax_dashboard/entities') . '</li>';
$output .= '<li>' . t('Check off the entities and bundles you want to place dashboards on.') . '</li>';
$output .= '<li>' . t('On /admin/config/system/ajax_dashboard you will see a new dashboard named [entity]__[bundle].') . '</li>';
$output .= '<li>' . t('This dashboard will be given four parameters:');
$output .= '<ul>';
$output .= '<li>' . t('entity: The Entity ID') . '</li>';
$output .= '<li>' . t('current_user: The ID of the current user') . '</li>';
$output .= '<li>' . t('entity_type: The entity type') . '</li>';
$output .= '<li>' . t('bundle: The bundle fo the entity') . '</li>';
$output .= '</ul>';
$output .= '</li>';
$output .= '<li>' . t('Once created, you will see an "Entity Dashboard" field in the entity display settings.') . '</li>';
$output .= '<li>' . t('Note: unchecking and re-saving will not delete the dashboard, but will disable it.') . '</li>';
$output .= '</ol>';
$output .= '<h3>' . t('Module hook') . '</h3>';
$output .= '<p>' . t('Use hook_ajax_dashboard_entity_dashboard() to specify entities and bundles that you want to require to have dashboards.') . '</p>';
$output .= '<p>' . t('You will still need to go to /admin/config/system/ajax_dashboard/entities and save one time to generate the dashboard.') . '</p>';
$output .= '<p>' . t('The hook should return an array in the following format') . '</p>';
$output .= '<pre>';
$output .= ' [
// Entity with bundles
entity_type_1 => [
bundle1 => bundle1,
bundle2 => bundle2
],
// Entity with no bundles, e.g. user
entity_type_2 => [
entity_type_2 => entity_type_2
]
]';
$output .= '</pre>';
return $output;
default:
}
}
/**
* Implements hook_entity_extra_field_info().
*/
function ajax_dashboard_entity_dashboard_entity_extra_field_info() {
$ret = [];
$config = \Drupal::configFactory()->getEditable('ajax_dashboard_entity_dashboard.settings');
$enabled_entities = $config->get('enabled_entities');
foreach ($enabled_entities as $type => $bundles) {
foreach ($bundles as $bundle => $value) {
$ret[$type][$bundle]['display']['ajax_dashboard_entity_dashboard'] = [
'label' => t('AJAX Dashboard: Entity Dashboard'),
'description' => t('Entity Dashboard'),
'weight' => 0,
];
}
}
return $ret;
}
/**
* Implements hook_entity_view().
*/
function ajax_dashboard_entity_dashboard_entity_view(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode) {
$type = $entity->getEntityTypeId();
$bundle = $entity->bundle();
$config = \Drupal::configFactory()->getEditable('ajax_dashboard_entity_dashboard.settings');
$enabled_entities = $config->get('enabled_entities');
if (isset($enabled_entities[$type][$bundle])) {
$dashboard_id = $type . '__' . $bundle;
if ($display->getComponent('ajax_dashboard_entity_dashboard')) {
$dashboard = [
'#theme' => 'ajax_dashboard',
'#id' => $dashboard_id,
'#params' => [
'entity' => $entity->id(),
'user' => \Drupal::currentUser()->id(),
'entity_type' => $type,
'bundle' => $bundle,
],
];
$build['ajax_dashboard_entity_dashboard'] = $dashboard;
}
}
}
