Repository URL to install this package:
|
Version:
1.0.20 ▾
|
novicell/atoms
/
atoms.module
|
|---|
<?php
use Drupal\Component\Utility\Html;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Render\Element;
use Drupal\Core\Render\Renderer;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Template\Attribute;
use Drupal\atoms\Atom;
use Drupal\atoms\AtomsViewBuilder;
use Drupal\atoms\AtomsBuilder;
use Drupal\atoms\AtomsManager;
use Drupal\atoms\ViewableAtom;
use Drupal\Core\Url;
/**
* Implements hook_locale_translation_projects_alter().
*/
function atoms_locale_translation_projects_alter(&$projects) {
$module_handler = \Drupal::service('module_handler');
$path = $module_handler->getModule('atoms')->getPath();
$projects['atoms']['info']['interface translation server pattern'] = $path.'/translations/%language.po';
}
/**
* Implements hook_modules_installed().
*
* Make sure to include any new atoms the new module may have.
*/
function atoms_modules_installed($modules) {
/** @var \Drupal\atoms\AtomsBuilder $builder */
$builder = \Drupal::service('atoms.builder');
$builder->rebuild();
}
/**
* Implements hook_theme().
*/
function atoms_theme() {
return [
'atom' => [
'render element' => 'elements',
],
'atom_element' => [
'render element' => 'element',
],
];
}
/**
* Implements hook_help().
*/
function atoms_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
// Main module help for the atoms module.
case 'help.page.atoms':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('Add atomic sized content.') . '</p>';
return $output;
default:
}
}
/**
* Implements hook_rebuild().
*/
function atoms_rebuild() {
/** @var AtomsBuilder $builder */
$builder = \Drupal::service('atoms.builder');
$builder->rebuild();
/** @var AtomsManager $manager */
$manager = \Drupal::service('plugin.atoms.manager');
$manager->setCachedAtomTypes();
}
/**
* Returns a render array representation of the object.
*
* @param string $machine_name
* @param string|null $langcode
*
* @return array
* A render array to render the atom.
*/
function atom($machine_name, $langcode = NULL) {
return atom_view($machine_name, $langcode)->toRenderable();
}
/**
* Returns a rendered string version of the object.
*
* @param string $machine_name
* @param string|null $langcode
*
* @return string
*/
function atom_str($machine_name, $langcode = NULL) {
return atom_view($machine_name, $langcode)->toString();
}
/**
* Returns a ViewableAtom.
*
* @param string $machine_name
* @param string|null $langcode
*
* @return ViewableAtom
*/
function atom_view($machine_name, $langcode = NULL) {
/** @var AtomsViewBuilder $service */
static $service = NULL;
if (is_null($service)) {
$service = \Drupal::service('atoms');
}
return $service->get($machine_name, $langcode);
}
/**
* Applies CacheableMetadata from list of Atoms to render array.
*
* @param array $build
* @param string[] $machine_names
* @param bool $add_contextual_links
* @param string|null $langcode
*/
function atom_cache(array &$build, array $machine_names, $add_contextual_links = FALSE, $langcode = NULL) {
$cacheableMetadata = CacheableMetadata::createFromRenderArray($build);
foreach ($machine_names as $machine_name) {
$cacheableMetadata = $cacheableMetadata->merge(atom_view($machine_name, $langcode)->getCacheableMetadata());
}
if ($add_contextual_links && !empty($machine_names)) {
if (empty($build['#contextual_links'])) {
$build['#contextual_links'] = [];
}
$build['#contextual_links']['atoms'] = [
'route_parameters' => ['group_id' => atom_view($machine_names[0], $langcode)->getAtom()->getGroupID()],
];
}
$cacheableMetadata->applyTo($build);
return $cacheableMetadata;
}
/**
* Returns a lazy builder version of the object.
*
* @param string $machine_name
* @param string|null $langcode
*
* @return array
*/
function atom_lazy($machine_name, $langcode = NULL) {
/** @var AtomsViewBuilder $service */
$service = \Drupal::service('atoms');
return $service->getLazyBuilder($machine_name, $langcode);
}
function template_preprocess_atom(&$variables) {
/** @var Atom $atom */
$atom = $variables['elements']['#atom'];
if (!empty($atom)) {
$variables['atom'] = $atom;
$variables['type'] = $atom->getType();
$variables['data'] = $atom->getData();
}
// Helpful $content variable for templates.
$variables += ['content' => []];
foreach (Element::children($variables['elements']) as $key) {
$variables['content'][$key] = $variables['elements'][$key];
}
}
/**
* Implements hook_contextual_links_view_alter().
*
* Change Configure Blocks into off_canvas links.
*/
function atoms_contextual_links_view_alter(&$element, $items) {
if (isset($element['#links']['settings-tray-atoms-configure'])) {
// Place settings_tray link first.
$settings_tray_link = $element['#links']['settings-tray-atoms-configure'];
unset($element['#links']['settings-tray-atoms-configure']);
$element['#links'] = ['settings-tray-atoms-configure' => $settings_tray_link] + $element['#links'];
$element['#attached']['library'][] = 'core/drupal.dialog.off_canvas';
}
}
/**
* Implements hook_token_info().
*
* @see hook_token_info()
*/
function atoms_token_info() {
$types = [
// [atoms:]
'atoms' => [
'name' => t('Atoms'),
'description' => t('Tokens for atoms.'),
]
];
$tokens = [
// [atoms:]
'atoms' => [
]
];
/** @var \Drupal\atoms\AtomsStorage $storage */
$storage = \Drupal::service('atoms.storage');
$machine_names = $storage->getMachineNames();
$atoms = Atom::loadMultiple($machine_names);
foreach ($atoms as $atom) {
$tokens['atoms'][$atom->getMachineName()] = [
'name' => t($atom->getTitle()),
'description' => t($atom->getCategory()) . ' -> ' . t($atom->getGroupName()) . ' -> ' . t($atom->getTitle()),
];
}
return array(
'types' => $types,
'tokens' => $tokens,
);
}
/**
* Implements hook_tokens().
*
* @param $type
* The machine-readable name of the type (group) of token being replaced, such
* as 'node', 'user', or another type defined by a hook_token_info()
* implementation.
* @param $tokens
* An array of tokens to be replaced. The keys are the machine-readable token
* names, and the values are the raw [type:token] strings that appeared in the
* original text.
* @param array $data
* An associative array of data objects to be used when generating replacement
* values, as supplied in the $data parameter to
* \Drupal\Core\Utility\Token::replace().
* @param array $options
* An associative array of options for token replacement; see
* \Drupal\Core\Utility\Token::replace() for possible values.
* @param \Drupal\Core\Render\BubbleableMetadata $bubbleable_metadata
* The bubbleable metadata. Prior to invoking this hook,
* \Drupal\Core\Utility\Token::generate() collects metadata for all of the
* data objects in $data. For any data sources not in $data, but that are
* used by the token replacement logic, such as global configuration (e.g.,
* 'system.site') and related objects (e.g., $node->getOwner()),
* implementations of this hook must add the corresponding metadata.
*
* @see hook_tokens()
*/
function atoms_tokens($type, $tokens, array $data, array $options, \Drupal\Core\Render\BubbleableMetadata $bubbleable_metadata) {
if (isset($options['langcode'])) {
$langcode = $options['langcode'];
}
else {
$langcode = NULL;
}
$replacements = [];
if ($type == 'atoms') {
foreach ($tokens as $machine_name => $original) {
$viewable = atom_view($machine_name, $langcode);
$replacements[$original] = $viewable->toString();
$bubbleable_metadata->addCacheableDependency($viewable);
}
}
return $replacements;
}
/**
* Massage atom transforms
*
* @param array $transformation The transform to be massaged.
*/
function atoms_atom_transform_alter(&$transformation) {
switch ($transformation['atom_type']) {
case 'button':
case 'link':
try {
$url = Url::fromUri($transformation['uri'] ?? '');
} catch (InvalidArgumentException $exception) {
\Drupal::logger('atoms')
->error('Atom (' . $transformation['machine_name'] . ') of type ' . $transformation['atom_type'] . ' has invalid data');
$url = Url::fromUri('internal:/');
}
$transformation['url'] = $url->toString();
break;
}
}