farm-2.x-dev/modules/core/asset/asset.module
modules/core/asset/asset.module
<?php
/**
* @file
* Hooks and customizations for the asset module.
*/
use Drupal\asset\Entity\AssetInterface;
use Drupal\asset\Event\AssetEvent;
use Drupal\Core\Render\Element;
use Drupal\Core\Routing\RouteMatchInterface;
/**
* Implements hook_help().
*/
function asset_help($route_name, RouteMatchInterface $route_match) {
$output = '';
// Main module help for the asset module.
if ($route_name == 'help.page.asset') {
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('Provides asset entity') . '</p>';
}
return $output;
}
/**
* Implements hook_ENTITY_TYPE_presave().
*/
function asset_asset_presave(AssetInterface $asset) {
// Dispatch an event on asset presave.
// @todo Replace this with core event via https://www.drupal.org/node/2551893.
$event = new AssetEvent($asset);
$event_dispatcher = \Drupal::service('event_dispatcher');
$event_dispatcher->dispatch($event, AssetEvent::PRESAVE);
}
/**
* Implements hook_ENTITY_TYPE_insert().
*/
function asset_asset_insert(AssetInterface $asset) {
// Dispatch an event on asset insert.
// @todo Replace this with core event via https://www.drupal.org/node/2551893.
$event = new AssetEvent($asset);
$event_dispatcher = \Drupal::service('event_dispatcher');
$event_dispatcher->dispatch($event, AssetEvent::INSERT);
}
/**
* Implements hook_ENTITY_TYPE_update().
*/
function asset_asset_update(AssetInterface $asset) {
// Dispatch an event on asset update.
// @todo Replace this with core event via https://www.drupal.org/node/2551893.
$event = new AssetEvent($asset);
$event_dispatcher = \Drupal::service('event_dispatcher');
$event_dispatcher->dispatch($event, AssetEvent::UPDATE);
}
/**
* Implements hook_ENTITY_TYPE_delete().
*/
function asset_asset_delete(AssetInterface $asset) {
// Dispatch an event on asset delete.
// @todo Replace this with core event via https://www.drupal.org/node/2551893.
$event = new AssetEvent($asset);
$event_dispatcher = \Drupal::service('event_dispatcher');
$event_dispatcher->dispatch($event, AssetEvent::DELETE);
}
/**
* Implements hook_theme().
*/
function asset_theme() {
return [
'asset' => [
'render element' => 'elements',
],
];
}
/**
* Implements hook_theme_suggestions_HOOK().
*/
function asset_theme_suggestions_asset(array $variables) {
$suggestions = [];
$asset = $variables['elements']['#asset'];
$sanitized_view_mode = strtr($variables['elements']['#view_mode'], '.', '_');
$suggestions[] = 'asset__' . $sanitized_view_mode;
$suggestions[] = 'asset__' . $asset->bundle();
$suggestions[] = 'asset__' . $asset->bundle() . '__' . $sanitized_view_mode;
$suggestions[] = 'asset__' . $asset->id();
$suggestions[] = 'asset__' . $asset->id() . '__' . $sanitized_view_mode;
return $suggestions;
}
/**
* Prepares variables for asset templates.
*
* Default template: asset.html.twig.
*
* @param array $variables
* An associative array containing:
* - elements: An associative array containing the asset information and any
* fields attached to the asset. Properties used:
* - #asset: A \Drupal\asset\Entity\Asset object. The asset entity.
* - attributes: HTML attributes for the containing element.
*/
function template_preprocess_asset(array &$variables) {
$variables['asset'] = $variables['elements']['#asset'];
// Helpful $content variable for templates.
foreach (Element::children($variables['elements']) as $key) {
$variables['content'][$key] = $variables['elements'][$key];
}
}
