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