X-Git-Url: http://www.aleph1.co.uk/gitweb/?a=blobdiff_plain;f=web%2Fmodules%2Fcontrib%2Flinkit%2Fsrc%2FElement%2FLinkit.php;fp=web%2Fmodules%2Fcontrib%2Flinkit%2Fsrc%2FElement%2FLinkit.php;h=25e9a0f4f99e343c31823ba83a54df0e2b2b4459;hb=a2bd1bf0c2c1f1a17d188f4dc0726a45494cefae;hp=0000000000000000000000000000000000000000;hpb=57c063afa3f66b07c4bbddc2d6129a96d90f0aad;p=yaffs-website diff --git a/web/modules/contrib/linkit/src/Element/Linkit.php b/web/modules/contrib/linkit/src/Element/Linkit.php new file mode 100644 index 000000000..25e9a0f4f --- /dev/null +++ b/web/modules/contrib/linkit/src/Element/Linkit.php @@ -0,0 +1,108 @@ + TRUE, + '#size' => 60, + '#process' => array( + array($class, 'processLinkitAutocomplete'), + array($class, 'processGroup'), + ), + '#pre_render' => array( + array($class, 'preRenderLinkitElement'), + array($class, 'preRenderGroup'), + ), + '#theme' => 'input__textfield', + '#theme_wrappers' => array('form_element'), + ); + } + + /** + * {@inheritdoc} + */ + public static function valueCallback(&$element, $input, FormStateInterface $form_state) { + return Textfield::valueCallback($element, $input, $form_state); + } + + /** + * Adds linkit custom autocomplete functionality to elements. + * + * Instead of using the core autocomplete, we use our own. + * + * {@inheritdoc} + * + * @see \Drupal\Core\Render\Element\FormElement::processAutocomplete + */ + public static function processLinkitAutocomplete(&$element, FormStateInterface $form_state, &$complete_form) { + $url = NULL; + $access = FALSE; + + if (!empty($element['#autocomplete_route_name'])) { + $parameters = isset($element['#autocomplete_route_parameters']) ? $element['#autocomplete_route_parameters'] : array(); + $url = Url::fromRoute($element['#autocomplete_route_name'], $parameters)->toString(TRUE); + /** @var \Drupal\Core\Access\AccessManagerInterface $access_manager */ + $access_manager = \Drupal::service('access_manager'); + $access = $access_manager->checkNamedRoute($element['#autocomplete_route_name'], $parameters, \Drupal::currentUser(), TRUE); + } + + if ($access) { + $metadata = BubbleableMetadata::createFromRenderArray($element); + if ($access->isAllowed()) { + $element['#attributes']['class'][] = 'form-linkit-autocomplete'; + $metadata->addAttachments(['library' => ['linkit/linkit.autocomplete']]); + // Provide a data attribute for the JavaScript behavior to bind to. + $element['#attributes']['data-autocomplete-path'] = $url->getGeneratedUrl(); + $metadata = $metadata->merge($url); + } + $metadata + ->merge(BubbleableMetadata::createFromObject($access)) + ->applyTo($element); + } + + return $element; + } + + /** + * Prepares a #type 'linkit' render element for input.html.twig. + * + * @param array $element + * An associative array containing the properties of the element. + * Properties used: #title, #value, #description, #size, #attributes. + * + * @return array + * The $element with prepared variables ready for input.html.twig. + */ + public static function preRenderLinkitElement($element) { + $element['#attributes']['type'] = 'text'; + Element::setAttributes($element, array('id', 'name', 'value', 'size')); + static::setAttributes($element, array('form-text')); + + return $element; + } + +}