3 namespace Drupal\contextual\Element;
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;
12 * Provides a contextual_links_placeholder element.
14 * @RenderElement("contextual_links_placeholder")
16 class ContextualLinksPlaceholder extends RenderElement {
21 public function getInfo() {
22 $class = get_class($this);
25 [$class, 'preRenderPlaceholder'],
32 * Pre-render callback: Renders a contextual links placeholder into #markup.
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
39 * @param array $element
40 * A structured array with #id containing a "contextual id".
43 * The passed-in element with a contextual link placeholder in '#markup'.
45 * @see _contextual_links_to_id()
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,
53 $element['#markup'] = new FormattableMarkup('<div@attributes></div>', ['@attributes' => $attribute]);