function OEmbedWidget::formElement

Same name in other branches
  1. 8.9.x core/modules/media/src/Plugin/Field/FieldWidget/OEmbedWidget.php \Drupal\media\Plugin\Field\FieldWidget\OEmbedWidget::formElement()
  2. 10 core/modules/media/src/Plugin/Field/FieldWidget/OEmbedWidget.php \Drupal\media\Plugin\Field\FieldWidget\OEmbedWidget::formElement()
  3. 11.x core/modules/media/src/Plugin/Field/FieldWidget/OEmbedWidget.php \Drupal\media\Plugin\Field\FieldWidget\OEmbedWidget::formElement()

Overrides StringTextfieldWidget::formElement

File

core/modules/media/src/Plugin/Field/FieldWidget/OEmbedWidget.php, line 32

Class

OEmbedWidget
Plugin implementation of the 'oembed_textfield' widget.

Namespace

Drupal\media\Plugin\Field\FieldWidget

Code

public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
    $element = parent::formElement($items, $delta, $element, $form, $form_state);
    
    /** @var \Drupal\media\Plugin\media\Source\OEmbedInterface $source */
    $source = $items->getEntity()
        ->getSource();
    $message = $this->t('You can link to media from the following services: @providers', [
        '@providers' => implode(', ', $source->getProviders()),
    ]);
    if (!empty($element['value']['#description'])) {
        $element['value']['#description'] = [
            '#theme' => 'item_list',
            '#items' => [
                $element['value']['#description'],
                $message,
            ],
        ];
    }
    else {
        $element['value']['#description'] = $message;
    }
    return $element;
}

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