25e9a0f4f99e343c31823ba83a54df0e2b2b4459
[yaffs-website] / web / modules / contrib / linkit / src / Element / Linkit.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\linkit\Element\Linkit.
6  */
7
8 namespace Drupal\linkit\Element;
9
10 use Drupal\Core\Form\FormStateInterface;
11 use Drupal\Core\Render\BubbleableMetadata;
12 use Drupal\Core\Render\Element;
13 use Drupal\Core\Render\Element\FormElement;
14 use Drupal\Core\Render\Element\Textfield;
15 use Drupal\Core\Url;
16
17 /**
18  * Provides a form element for linkit.
19  *
20  * @FormElement("linkit")
21  */
22 class Linkit extends FormElement {
23
24   /**
25    * {@inheritdoc}
26    */
27   public function getInfo() {
28     $class = get_class($this);
29     return array(
30       '#input' => TRUE,
31       '#size' => 60,
32       '#process' => array(
33         array($class, 'processLinkitAutocomplete'),
34         array($class, 'processGroup'),
35       ),
36       '#pre_render' => array(
37         array($class, 'preRenderLinkitElement'),
38         array($class, 'preRenderGroup'),
39       ),
40       '#theme' => 'input__textfield',
41       '#theme_wrappers' => array('form_element'),
42     );
43   }
44
45   /**
46    * {@inheritdoc}
47    */
48   public static function valueCallback(&$element, $input, FormStateInterface $form_state) {
49     return Textfield::valueCallback($element, $input, $form_state);
50   }
51
52   /**
53    * Adds linkit custom autocomplete functionality to elements.
54    *
55    * Instead of using the core autocomplete, we use our own.
56    *
57    * {@inheritdoc}
58    *
59    * @see \Drupal\Core\Render\Element\FormElement::processAutocomplete
60    */
61   public static function processLinkitAutocomplete(&$element, FormStateInterface $form_state, &$complete_form) {
62     $url = NULL;
63     $access = FALSE;
64
65     if (!empty($element['#autocomplete_route_name'])) {
66       $parameters = isset($element['#autocomplete_route_parameters']) ? $element['#autocomplete_route_parameters'] : array();
67       $url = Url::fromRoute($element['#autocomplete_route_name'], $parameters)->toString(TRUE);
68       /** @var \Drupal\Core\Access\AccessManagerInterface $access_manager */
69       $access_manager = \Drupal::service('access_manager');
70       $access = $access_manager->checkNamedRoute($element['#autocomplete_route_name'], $parameters, \Drupal::currentUser(), TRUE);
71     }
72
73     if ($access) {
74       $metadata = BubbleableMetadata::createFromRenderArray($element);
75       if ($access->isAllowed()) {
76         $element['#attributes']['class'][] = 'form-linkit-autocomplete';
77         $metadata->addAttachments(['library' => ['linkit/linkit.autocomplete']]);
78         // Provide a data attribute for the JavaScript behavior to bind to.
79         $element['#attributes']['data-autocomplete-path'] = $url->getGeneratedUrl();
80         $metadata = $metadata->merge($url);
81       }
82       $metadata
83         ->merge(BubbleableMetadata::createFromObject($access))
84         ->applyTo($element);
85     }
86
87     return $element;
88   }
89
90   /**
91    * Prepares a #type 'linkit' render element for input.html.twig.
92    *
93    * @param array $element
94    *   An associative array containing the properties of the element.
95    *   Properties used: #title, #value, #description, #size, #attributes.
96    *
97    * @return array
98    *   The $element with prepared variables ready for input.html.twig.
99    */
100   public static function preRenderLinkitElement($element) {
101     $element['#attributes']['type'] = 'text';
102     Element::setAttributes($element, array('id', 'name', 'value', 'size'));
103     static::setAttributes($element, array('form-text'));
104
105     return $element;
106   }
107
108 }