Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / contextual / src / Element / ContextualLinksPlaceholder.php
1 <?php
2
3 namespace Drupal\contextual\Element;
4
5 use Drupal\Component\Utility\Crypt;
6 use Drupal\Core\Site\Settings;
7 use Drupal\Core\Template\Attribute;
8 use Drupal\Core\Render\Element\RenderElement;
9 use Drupal\Component\Render\FormattableMarkup;
10
11 /**
12  * Provides a contextual_links_placeholder element.
13  *
14  * @RenderElement("contextual_links_placeholder")
15  */
16 class ContextualLinksPlaceholder extends RenderElement {
17
18   /**
19    * {@inheritdoc}
20    */
21   public function getInfo() {
22     $class = get_class($this);
23     return [
24       '#pre_render' => [
25         [$class, 'preRenderPlaceholder'],
26       ],
27       '#id' => NULL,
28     ];
29   }
30
31   /**
32    * Pre-render callback: Renders a contextual links placeholder into #markup.
33    *
34    * Renders an empty (hence invisible) placeholder div with a data-attribute
35    * that contains an identifier ("contextual id"), which allows the JavaScript
36    * of the drupal.contextual-links library to dynamically render contextual
37    * links.
38    *
39    * @param array $element
40    *   A structured array with #id containing a "contextual id".
41    *
42    * @return array
43    *   The passed-in element with a contextual link placeholder in '#markup'.
44    *
45    * @see _contextual_links_to_id()
46    */
47   public static function preRenderPlaceholder(array $element) {
48     $token = Crypt::hmacBase64($element['#id'], Settings::getHashSalt() . \Drupal::service('private_key')->get());
49     $attribute = new Attribute([
50       'data-contextual-id' => $element['#id'],
51       'data-contextual-token' => $token,
52     ]);
53     $element['#markup'] = new FormattableMarkup('<div@attributes></div>', ['@attributes' => $attribute]);
54
55     return $element;
56   }
57
58 }