ajax_dashboard-8.x-2.x-dev/modules/ajax_dashboard_example/ajax_dashboard_example.module
modules/ajax_dashboard_example/ajax_dashboard_example.module
<?php
/**
* @file
* AJAX Dashboard Example: Module hooks.
*/
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Routing\RouteMatchInterface;
/**
* Implements hook_help().
*/
function ajax_dashboard_example_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
// Main module help for the ajax_dashboard module.
case 'help.page.ajax_dashboard_example':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('Example of an implementation of the AJAX Dashboard module.') . '</p>';
return $output;
default:
}
}
/**
* Implements ajax_dashboard_entity_dashboard().
*/
function ajax_dashboard_example_ajax_dashboard_entity_dashboard() {
return [
'user' => ['user' => 'user']
];
}
/**
* Implements hook_entity_extra_field_info().
*/
function ajax_dashboard_example_entity_extra_field_info() {
$ret = [];
$ret['user']['user']['display']['ajax_dashboard_example_sidebar'] = [
'label' => t('AJAX Dashboard Example: Sidebar Controls'),
'description' => t('AJAX Dashboard Example'),
'weight' => 10,
];
$ret['user']['user']['display']['ajax_dashboard_example_bottom'] = [
'label' => t('AJAX Dashboard Example: Bottom Controls'),
'description' => t('AJAX Dashboard Example'),
'weight' => 10,
];
return $ret;
}
/**
* Implements hook_ENTITY_TYPE_view().
*/
function ajax_dashboard_example_user_view(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode) {
foreach (['ajax_dashboard_example_sidebar', 'ajax_dashboard_example_bottom'] as $dashboard_id) {
if ($display->getComponent($dashboard_id)) {
$dashboard = [
'#theme' => 'ajax_dashboard',
'#id' => $dashboard_id,
'#params' => [],
];
$build[$dashboard_id] = $dashboard;
}
}
}
/**
* Implements hook_ajax_dashboard_alter().
*/
function ajax_dashboard_example_ajax_dashboard_alter(&$dashboard) {
// You may select a dashboard by selecting its $dashboard['id'],
// and modify its structure.
if ($dashboard['id'] === 'ajax_dashboard_example_sidebar') {
// Example: add a short Lorem Ipsum text.
$dashboard['controls']['ajax_dashboard_example_sidebar_first']['buttons']['lipsum'] = [
'plugin' => 'constant',
'label' => 'Lorem Ipsum Text',
'constant' => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.',
];
}
}
/**
* Implements hook_theme().
*
* Templates in modules, including extensions of existing templates, must be
* registered with hook_theme().
*/
function ajax_dashboard_example_theme() {
return [
'ajax_dashboard__ajax_dashboard_example_bottom' => [
'base hook' => 'ajax_dashboard',
],
'ajax_dashboard__ajax_dashboard_example_sidebar' => [
'base hook' => 'ajax_dashboard',
],
'ajax_dashboard_control__ajax_dashboard_example_bottom' => [
'base hook' => 'ajax_dashboard_control',
],
'ajax_dashboard_control__ajax_dashboard_example_sidebar' => [
'base hook' => 'ajax_dashboard_control',
],
];
}
