*/
use Drupal\Component\Render\PlainTextOutput;
-use Drupal\Component\Utility\Unicode;
use Drupal\Core\Block\BlockPluginInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Menu\MenuLinkInterface;
* Return an array of the core modules supported by token.module.
*/
function _token_core_supported_modules() {
- return array('book', 'field', 'menu_ui');
+ return ['book', 'field', 'menu_ui'];
}
/**
'#token_types' => [],
];
$rendered_token_tree = \Drupal::service('renderer')->render($token_tree);
- $form['settings']['label']['#description'] =
- t('This field supports tokens. @browse_tokens_link', ['@browse_tokens_link' => $rendered_token_tree])
- ;
+ $form['settings']['label']['#description'] = t('This field supports tokens. @browse_tokens_link', [
+ '@browse_tokens_link' => $rendered_token_tree,
+ ]);
$form['settings']['label']['#element_validate'][] = 'token_element_validate';
$form['settings']['label'] += ['#token_types' => []];
}
* Implements hook_field_info_alter().
*/
function token_field_info_alter(&$info) {
- $defaults = array(
+ $defaults = [
'taxonomy_term_reference' => 'taxonomy_term_reference_plain',
'number_integer' => 'number_unformatted',
'number_decimal' => 'number_unformatted',
'list_float' => 'list_default',
'list_string' => 'list_default',
'list_boolean' => 'list_default',
- );
+ ];
foreach ($defaults as $field_type => $default_token_formatter) {
if (isset($info[$field_type])) {
- $info[$field_type] += array('default_token_formatter' => $default_token_formatter);
+ $info[$field_type] += ['default_token_formatter' => $default_token_formatter];
}
}
}
// other modules.
if (isset($implementations['token'])) {
unset($implementations['token']);
- $implementations = array_merge(array('token' => 'tokens'), $implementations);
+ $implementations = array_merge(['token' => 'tokens'], $implementations);
}
}
}
*
* For example:
* @code
- * $form['my_node_text_element'] = array(
+ * $form['my_node_text_element'] = [
* '#type' => 'textfield',
* '#title' => t('Some text to token-ize that has a node context.'),
* '#default_value' => 'The title of this node is [node:title].',
- * '#element_validate' => array('token_element_validate'),
- * '#token_types' => array('node'),
+ * '#element_validate' => ['token_element_validate'],
+ * '#token_types' => ['node'],
* '#min_tokens' => 1,
* '#max_tokens' => 10,
- * );
+ * ];
* @endcode
*/
function token_element_validate($element, FormStateInterface $form_state) {
$value = isset($element['#value']) ? $element['#value'] : $element['#default_value'];
- if (!Unicode::strlen($value)) {
+ if (!mb_strlen($value)) {
// Empty value needs no further validation since the element should depend
// on using the '#required' FAPI property.
return $element;
// Validate if an element must have a minimum number of tokens.
if (isset($element['#min_tokens']) && count($tokens) < $element['#min_tokens']) {
- $error = \Drupal::translation()->formatPlural($element['#min_tokens'], '%name must contain at least one token.', '%name must contain at least @count tokens.', array('%name' => $title));
+ $error = \Drupal::translation()->formatPlural($element['#min_tokens'], '%name must contain at least one token.', '%name must contain at least @count tokens.', ['%name' => $title]);
$form_state->setError($element, $error);
}
// Validate if an element must have a maximum number of tokens.
if (isset($element['#max_tokens']) && count($tokens) > $element['#max_tokens']) {
- $error = \Drupal::translation()->formatPlural($element['#max_tokens'], '%name must contain at most one token.', '%name must contain at most @count tokens.', array('%name' => $title));
+ $error = \Drupal::translation()->formatPlural($element['#max_tokens'], '%name must contain at most one token.', '%name must contain at most @count tokens.', ['%name' => $title]);
$form_state->setError($element, $error);
}
if (isset($element['#token_types'])) {
$invalid_tokens = \Drupal::token()->getInvalidTokensByContext($tokens, $element['#token_types']);
if ($invalid_tokens) {
- $form_state->setError($element, t('%name is using the following invalid tokens: @invalid-tokens.', array('%name' => $title, '@invalid-tokens' => implode(', ', $invalid_tokens))));
+ $form_state->setError($element, t('%name is using the following invalid tokens: @invalid-tokens.', ['%name' => $title, '@invalid-tokens' => implode(', ', $invalid_tokens)]));
}
}
// Date support needs to be implicitly added, as while technically it's not
// a global token, it is a not only used but is the default value.
// https://www.drupal.org/node/2642160
- $form['settings']['file_directory'] += array('#token_types' => array('date'));
+ $form['settings']['file_directory'] += ['#token_types' => ['date']];
$form['settings']['file_directory']['#description'] .= ' ' . t('This field supports tokens.');
}
// Note that the description is tokenized via token_field_widget_form_alter().
$form['description']['#element_validate'][] = 'token_element_validate';
- $form['description'] += array('#token_types' => array());
+ $form['description'] += ['#token_types' => []];
- $form['token_tree'] = array(
+ $form['token_tree'] = [
'#theme' => 'token_tree_link',
- '#token_types' => array(),
+ '#token_types' => [],
'#weight' => $form['description']['#weight'] + 0.5,
- );
+ ];
}
/**
case 'email_cancel_confirm':
// Do nothing, but allow execution to continue.
break;
+
case 'email_activated':
case 'email_blocked':
case 'email_canceled':
// sub-element, so switch to that element instead.
$element = &$form[$key]['settings'];
break;
+
default:
continue 2;
}
elseif ($element[$sub_key]['#type'] == 'textfield' && substr($sub_key, -8) === '_subject') {
// Add validation to subject textfields.
$element[$sub_key]['#element_validate'][] = 'token_element_validate';
- $element[$sub_key] += array('#token_types' => array('user'));
+ $element[$sub_key] += ['#token_types' => ['user']];
}
elseif ($element[$sub_key]['#type'] == 'textarea' && substr($sub_key, -5) === '_body') {
// Add validation to body textareas.
$element[$sub_key]['#element_validate'][] = 'token_element_validate';
- $element[$sub_key] += array('#token_types' => array('user'));
+ $element[$sub_key] += ['#token_types' => ['user']];
}
}
}
// Add the token tree UI.
- $form['email']['token_tree'] = array(
+ $form['email']['token_tree'] = [
'#theme' => 'token_tree_link',
- '#token_types' => array('user'),
+ '#token_types' => ['user'],
'#show_restricted' => TRUE,
'#show_nested' => FALSE,
'#weight' => 90,
- );
+ ];
}
/**
* The cleaned token name.
*/
function token_clean_token_name($name) {
- static $names = array();
+ static $names = [];
if (!isset($names[$name])) {
- $cleaned_name = strtr($name, array(' ' => '-', '_' => '-', '/' => '-', '[' => '-', ']' => ''));
+ $cleaned_name = strtr($name, [' ' => '-', '_' => '-', '/' => '-', '[' => '-', ']' => '']);
$cleaned_name = preg_replace('/[^\w\-]/i', '', $cleaned_name);
$cleaned_name = trim($cleaned_name, '-');
$names[$name] = $cleaned_name;
/**
* Do not use this function yet. Its API has not been finalized.
*/
-function token_render_array(array $array, array $options = array()) {
- $rendered = array();
+function token_render_array(array $array, array $options = []) {
+ $rendered = [];
/** @var \Drupal\Core\Render\RendererInterface $renderer */
$renderer = \Drupal::service('renderer');
/**
* Do not use this function yet. Its API has not been finalized.
*/
-function token_render_array_value($value, array $options = array()) {
+function token_render_array_value($value, array $options = []) {
/** @var \Drupal\Core\Render\RendererInterface $renderer */
$renderer = \Drupal::service('renderer');
*/
function token_render_cache_set(&$markup, $elements) {
// This should only run of drupal_render_cache_set() did not.
- if (in_array(\Drupal::request()->server->get('REQUEST_METHOD'), array('GET', 'HEAD'))) {
+ if (in_array(\Drupal::request()->server->get('REQUEST_METHOD'), ['GET', 'HEAD'])) {
return FALSE;
}
* List of menu link parent titles.
*/
function token_menu_link_load_all_parents($plugin_id, $langcode) {
- $cache = &drupal_static(__FUNCTION__, array());
+ $cache = &drupal_static(__FUNCTION__, []);
if (!isset($cache[$plugin_id][$langcode])) {
- $cache[$plugin_id][$langcode] = array();
+ $cache[$plugin_id][$langcode] = [];
/** @var \Drupal\Core\Menu\MenuLinkManagerInterface $menu_link_manager */
$menu_link_manager = \Drupal::service('plugin.manager.menu.link');
$parent_ids = $menu_link_manager->getParentIds($plugin_id);
unset($parent_ids[$plugin_id]);
foreach ($parent_ids as $parent_id) {
$parent = $menu_link_manager->createInstance($parent_id);
- $cache[$plugin_id][$langcode] = array($parent_id => token_menu_link_translated_title($parent, $langcode)) + $cache[$plugin_id][$langcode];
+ $cache[$plugin_id][$langcode] = [$parent_id => token_menu_link_translated_title($parent, $langcode)] + $cache[$plugin_id][$langcode];
}
}
* The term parents collection.
*/
function token_taxonomy_term_load_all_parents($tid, $langcode) {
- $cache = &drupal_static(__FUNCTION__, array());
+ $cache = &drupal_static(__FUNCTION__, []);
if (!is_numeric($tid)) {
- return array();
+ return [];
}
if (!isset($cache[$langcode][$tid])) {
- $cache[$langcode][$tid] = array();
+ $cache[$langcode][$tid] = [];
/** @var \Drupal\taxonomy\TermStorageInterface $term_storage */
$term_storage = \Drupal::entityTypeManager()->getStorage('taxonomy_term');
$parents = $term_storage->loadAllParents($tid);
$sort = isset($elements['#sorted']) ? !$elements['#sorted'] : $sort;
// Filter out properties from the element, leaving only children.
- $children = array();
+ $children = [];
$sortable = FALSE;
foreach ($elements as $key => $value) {
if ($key === '' || $key[0] !== '#') {
* List of node titles of the book parents.
*/
function token_book_load_all_parents(array $book) {
- $cache = &drupal_static(__FUNCTION__, array());
+ $cache = &drupal_static(__FUNCTION__, []);
if (empty($book['nid'])) {
- return array();
+ return [];
}
$nid = $book['nid'];
if (!isset($cache[$nid])) {
- $cache[$nid] = array();
+ $cache[$nid] = [];
$i = 1;
while ($book["p$i"] != $nid) {
$cache[$nid][] = Node::load($book["p$i"])->getTitle();
* Implements hook_entity_base_field_info().
*/
function token_entity_base_field_info(EntityTypeInterface $entity_type) {
- // We add a psuedo entity-reference field to track the menu entry created
+ // We add a pseudo entity-reference field to track the menu entry created
// from the node add/edit form so that tokens generated at that time that
// reference the menu link can access the yet to be saved menu link.
// @todo Revisit when https://www.drupal.org/node/2315773 is resolved.
->setRevisionable(TRUE)
->setSetting('target_type', 'menu_link_content')
->setTranslatable(TRUE)
- ->setDisplayOptions('view', array(
+ ->setDisplayOptions('view', [
'label' => 'hidden',
'region' => 'hidden',
- ))
+ ])
->setComputed(TRUE)
- ->setDisplayOptions('form', array(
+ ->setDisplayOptions('form', [
'region' => 'hidden',
- ));
+ ]);
return $fields;
}
else {
if ($node->isNew()) {
// Create a new menu_link_content entity.
- $entity = MenuLinkContent::create(array(
+ $entity = MenuLinkContent::create([
// Lets just reference the UUID for now, the link is not important for
// token generation.
'link' => ['uri' => 'internal:/node/' . $node->uuid()],
'langcode' => $node->language()->getId(),
- ));
+ ]);
}
else {
// Create a new menu_link_content entity.
- $entity = MenuLinkContent::create(array(
+ $entity = MenuLinkContent::create([
'link' => ['uri' => 'entity:node/' . $node->id()],
'langcode' => $node->language()->getId(),
- ));
+ ]);
}
}
$entity->title->value = trim($values['title']);
function token_node_insert(NodeInterface $node) {
if ($node->hasField('menu_link') && $menu_link = $node->menu_link->entity) {
// Update the menu-link to point to the now saved node.
- $menu_link->link = 'entity:node/' . $node->id();
+ $menu_link->link = 'entity:node/' . $node->id();
$menu_link->save();
}
}