function PathWidget::formElement

Same name and namespace in other branches
  1. 9 core/modules/path/src/Plugin/Field/FieldWidget/PathWidget.php \Drupal\path\Plugin\Field\FieldWidget\PathWidget::formElement()
  2. 10 core/modules/path/src/Plugin/Field/FieldWidget/PathWidget.php \Drupal\path\Plugin\Field\FieldWidget\PathWidget::formElement()
  3. 11.x core/modules/path/src/Plugin/Field/FieldWidget/PathWidget.php \Drupal\path\Plugin\Field\FieldWidget\PathWidget::formElement()

Overrides WidgetInterface::formElement

File

core/modules/path/src/Plugin/Field/FieldWidget/PathWidget.php, line 26

Class

PathWidget
Plugin implementation of the 'path' widget.

Namespace

Drupal\path\Plugin\Field\FieldWidget

Code

public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
    $entity = $items->getEntity();
    $element += [
        '#element_validate' => [
            [
                get_class($this),
                'validateFormElement',
            ],
        ],
    ];
    $element['alias'] = [
        '#type' => 'textfield',
        '#title' => $element['#title'],
        '#default_value' => $items[$delta]->alias,
        '#required' => $element['#required'],
        '#maxlength' => 255,
        '#description' => $this->t('Specify an alternative path by which this data can be accessed. For example, type "/about" when writing an about page.'),
    ];
    $element['pid'] = [
        '#type' => 'value',
        '#value' => $items[$delta]->pid,
    ];
    $element['source'] = [
        '#type' => 'value',
        '#value' => !$entity->isNew() ? '/' . $entity->toUrl()
            ->getInternalPath() : NULL,
    ];
    $element['langcode'] = [
        '#type' => 'value',
        '#value' => $items[$delta]->langcode,
    ];
    // If the advanced settings tabs-set is available (normally rendered in the
    // second column on wide-resolutions), place the field as a details element
    // in this tab-set.
    if (isset($form['advanced'])) {
        $element += [
            '#type' => 'details',
            '#title' => t('URL path settings'),
            '#open' => !empty($items[$delta]->alias),
            '#group' => 'advanced',
            '#access' => $entity->get('path')
                ->access('edit'),
            '#attributes' => [
                'class' => [
                    'path-form',
                ],
            ],
            '#attached' => [
                'library' => [
                    'path/drupal.path',
                ],
            ],
        ];
        $element['#weight'] = 30;
    }
    return $element;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.