You are here

function linkicon_field_widget_link_default_form_alter in Link Icon 8

Implements hook_field_widget_WIDGET_TYPE_form_alter().

File

./linkicon.module, line 62
A link field formatter to create icon classes based on predefined titles.

Code

function linkicon_field_widget_link_default_form_alter(&$element, FormStateInterface $form_state, $context) {
  if (isset($element['uri']) && isset($element['title'])) {
    $items = $context['items'];

    // @todo drop 'predefined' for the new integer: LINKICON_PREDEFINED.
    $predefined = $items
      ->getSetting('title') === LinkIconManagerInterface::LINKICON_PREDEFINED || $items
      ->getSetting('title') === 'predefined';
    if ($predefined && ($title_predefined = $items
      ->getSetting('title_predefined')) !== NULL) {
      $options = linkicon()
        ->extractAllowedValues($title_predefined);
      $element['title']['#type'] = 'select';
      $element['title']['#options'] = $options;
      $element['title']['#empty_option'] = t('- Select -');
      $element['title']['#access'] = TRUE;
      $element['#element_validate'] = [
        '\\Drupal\\linkicon\\LinkIconItem::elementValidateLinkIcon',
      ];
    }
  }
}