class LinkWidget

Plugin implementation of the 'link' widget.

Plugin annotation


@Plugin(
  id = "link_default",
  module = "link",
  label = @Translation("Link"),
  field_types = {
    "link"
  },
  settings = {
    "placeholder_url" = "",
    "placeholder_title" = ""
  }
)

Hierarchy

Expanded class hierarchy of LinkWidget

File

drupal/core/modules/link/lib/Drupal/link/Plugin/field/widget/LinkWidget.php, line 30
Contains \Drupal\link\Plugin\field\widget\LinkWidget.

Namespace

Drupal\link\Plugin\field\widget
View source
class LinkWidget extends WidgetBase {

  /**
   * {@inheritdoc}
   */
  public function formElement(array $items, $delta, array $element, $langcode, array &$form, array &$form_state) {
    $instance = $this->instance;
    $element['url'] = array(
      '#type' => 'url',
      '#title' => t('URL'),
      '#placeholder' => $this
        ->getSetting('placeholder_url'),
      '#default_value' => isset($items[$delta]['url']) ? $items[$delta]['url'] : NULL,
      '#maxlength' => 2048,
      '#required' => $element['#required'],
    );
    $element['title'] = array(
      '#type' => 'textfield',
      '#title' => t('Link text'),
      '#placeholder' => $this
        ->getSetting('placeholder_title'),
      '#default_value' => isset($items[$delta]['title']) ? $items[$delta]['title'] : NULL,
      '#maxlength' => 255,
      '#access' => $instance['settings']['title'] != DRUPAL_DISABLED,
    );

    // Post-process the title field to make it conditionally required if URL is
    // non-empty. Omit the validation on the field edit form, since the field
    // settings cannot be saved otherwise.
    $is_field_edit_form = $element['#entity'] === NULL;
    if (!$is_field_edit_form && $instance['settings']['title'] == DRUPAL_REQUIRED) {
      $element['#element_validate'] = array(
        array(
          $this,
          'validateTitle',
        ),
      );
    }

    // Exposing the attributes array in the widget is left for alternate and more
    // advanced field widgets.
    $element['attributes'] = array(
      '#type' => 'value',
      '#tree' => TRUE,
      '#value' => !empty($items[$delta]['attributes']) ? $items[$delta]['attributes'] : array(),
      '#attributes' => array(
        'class' => array(
          'link-field-widget-attributes',
        ),
      ),
    );

    // If cardinality is 1, ensure a label is output for the field by wrapping it
    // in a details element.
    if ($this->field['cardinality'] == 1) {
      $element += array(
        '#type' => 'fieldset',
      );
    }
    return $element;
  }

  /**
   * {@inheritdoc}
   */
  public function settingsForm(array $form, array &$form_state) {
    $elements = parent::settingsForm($form, $form_state);
    $elements['placeholder_url'] = array(
      '#type' => 'textfield',
      '#title' => t('Placeholder for URL'),
      '#default_value' => $this
        ->getSetting('placeholder_url'),
      '#description' => t('Text that will be shown inside the field until a value is entered. This hint is usually a sample value or a brief description of the expected format.'),
    );
    $elements['placeholder_title'] = array(
      '#type' => 'textfield',
      '#title' => t('Placeholder for link text'),
      '#default_value' => $this
        ->getSetting('placeholder_title'),
      '#description' => t('Text that will be shown inside the field until a value is entered. This hint is usually a sample value or a brief description of the expected format.'),
      '#states' => array(
        'invisible' => array(
          ':input[name="instance[settings][title]"]' => array(
            'value' => DRUPAL_DISABLED,
          ),
        ),
      ),
    );
    return $elements;
  }

  /**
   * Form element validation handler for link_field_widget_form().
   *
   * Conditionally requires the link title if a URL value was filled in.
   */
  function validateTitle(&$element, &$form_state, $form) {
    if ($element['url']['#value'] !== '' && $element['title']['#value'] === '') {
      $element['title']['#required'] = TRUE;
      form_error($element['title'], t('!name field is required.', array(
        '!name' => $element['title']['#title'],
      )));
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
LinkWidget::formElement public function Returns the form for a single field widget. Overrides WidgetInterface::formElement
LinkWidget::settingsForm public function Implements Drupal\field\Plugin\Type\Widget\WidgetInterface::settingsForm(). Overrides WidgetBase::settingsForm
LinkWidget::validateTitle function Form element validation handler for link_field_widget_form().
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition.
PluginBase::$pluginId protected property The plugin_id.
PluginBase::getPluginDefinition public function Returns the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition
PluginBase::getPluginId public function Returns the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginSettingsBase::$defaultSettingsMerged protected property Whether default settings have been merged into the current $settings.
PluginSettingsBase::getDefaultSettings public function Implements Drupal\field\Plugin\PluginSettingsInterface::getDefaultSettings(). Overrides PluginSettingsInterface::getDefaultSettings
PluginSettingsBase::getSetting public function Implements Drupal\field\Plugin\PluginSettingsInterface::getSetting(). Overrides PluginSettingsInterface::getSetting
PluginSettingsBase::getSettings public function Implements Drupal\field\Plugin\PluginSettingsInterface::getSettings(). Overrides PluginSettingsInterface::getSettings
PluginSettingsBase::mergeDefaults protected function Merges default settings values into $settings.
PluginSettingsBase::setSetting public function Implements Drupal\field\Plugin\PluginSettingsInterface::setSetting(). Overrides PluginSettingsInterface::setSetting
PluginSettingsBase::setSettings public function Implements Drupal\field\Plugin\PluginSettingsInterface::setSettings(). Overrides PluginSettingsInterface::setSettings
WidgetBase::$field protected property The field definition.
WidgetBase::$instance protected property The field instance definition.
WidgetBase::$settings protected property The widget settings. Overrides PluginSettingsBase::$settings
WidgetBase::errorElement public function Implements Drupal\field\Plugin\Type\Widget\WidgetInterface::errorElement(). Overrides WidgetInterface::errorElement 5
WidgetBase::extractFormValues public function Implements Drupal\field\Plugin\Type\Widget\WidgetInterface::extractFormValues(). Overrides WidgetBaseInterface::extractFormValues
WidgetBase::flagErrors public function Implements Drupal\field\Plugin\Type\Widget\WidgetInterface::flagErrors(). Overrides WidgetBaseInterface::flagErrors
WidgetBase::form public function Implements Drupal\field\Plugin\Type\Widget\WidgetInterface::form(). Overrides WidgetBaseInterface::form
WidgetBase::formMultipleElements protected function Special handling to create form elements for multiple values. 1
WidgetBase::formSingleElement protected function Generates the form element for a single copy of the widget.
WidgetBase::massageFormValues public function Implements Drupal\field\Plugin\Type\Widget\WidgetInterface::massageFormValues() Overrides WidgetInterface::massageFormValues 2
WidgetBase::sortItems protected function Sorts submitted field values according to drag-n-drop reordering.
WidgetBase::__construct public function Constructs a WidgetBase object. Overrides PluginBase::__construct 3