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