Added another front page space for Yaffs info. Added roave security for composer.
[yaffs-website] / web / core / lib / Drupal / Core / Render / Element / Url.php
1 <?php
2
3 namespace Drupal\Core\Render\Element;
4
5 use Drupal\Component\Utility\UrlHelper;
6 use Drupal\Core\Form\FormStateInterface;
7 use Drupal\Core\Render\Element;
8
9 /**
10  * Provides a form element for input of a URL.
11  *
12  * Properties:
13  * - #default_value: A valid URL string.
14  * - #size: The size of the input element in characters.
15  *
16  * Usage example:
17  * @code
18  * $form['homepage'] = array(
19  *   '#type' => 'url',
20  *   '#title' => $this->t('Home Page'),
21  *   '#size' => 30,
22  *   ...
23  * );
24  * @endcode
25  *
26  * @see \Drupal\Core\Render\Element\Textfield
27  *
28  * @FormElement("url")
29  */
30 class Url extends FormElement {
31
32   /**
33    * {@inheritdoc}
34    */
35   public function getInfo() {
36     $class = get_class($this);
37     return [
38       '#input' => TRUE,
39       '#size' => 60,
40       '#maxlength' => 255,
41       '#autocomplete_route_name' => FALSE,
42       '#process' => [
43         [$class, 'processAutocomplete'],
44         [$class, 'processAjaxForm'],
45         [$class, 'processPattern'],
46       ],
47       '#element_validate' => [
48         [$class, 'validateUrl'],
49       ],
50       '#pre_render' => [
51         [$class, 'preRenderUrl'],
52       ],
53       '#theme' => 'input__url',
54       '#theme_wrappers' => ['form_element'],
55     ];
56   }
57
58   /**
59    * Form element validation handler for #type 'url'.
60    *
61    * Note that #maxlength and #required is validated by _form_validate() already.
62    */
63   public static function validateUrl(&$element, FormStateInterface $form_state, &$complete_form) {
64     $value = trim($element['#value']);
65     $form_state->setValueForElement($element, $value);
66
67     if ($value !== '' && !UrlHelper::isValid($value, TRUE)) {
68       $form_state->setError($element, t('The URL %url is not valid.', ['%url' => $value]));
69     }
70   }
71
72   /**
73    * Prepares a #type 'url' render element for input.html.twig.
74    *
75    * @param array $element
76    *   An associative array containing the properties of the element.
77    *   Properties used: #title, #value, #description, #size, #maxlength,
78    *   #placeholder, #required, #attributes.
79    *
80    * @return array
81    *   The $element with prepared variables ready for input.html.twig.
82    */
83   public static function preRenderUrl($element) {
84     $element['#attributes']['type'] = 'url';
85     Element::setAttributes($element, ['id', 'name', 'value', 'size', 'maxlength', 'placeholder']);
86     static::setAttributes($element, ['form-url']);
87
88     return $element;
89   }
90
91 }