diff --git a/core/modules/link/src/Plugin/Field/FieldFormatter/LinkFormatter.php b/core/modules/link/src/Plugin/Field/FieldFormatter/LinkFormatter.php index 8819fe3..4686285 100644 --- a/core/modules/link/src/Plugin/Field/FieldFormatter/LinkFormatter.php +++ b/core/modules/link/src/Plugin/Field/FieldFormatter/LinkFormatter.php @@ -185,6 +185,7 @@ public function viewElements(FieldItemListInterface $items) { // By default use the full URL as the link text. $url = $this->buildUrl($item); $link_title = $url->toString(); + $link_description = ''; // If the title field value is available, use it for the link text. if (empty($settings['url_only']) && !empty($item->title)) { @@ -193,6 +194,13 @@ public function viewElements(FieldItemListInterface $items) { $link_title = \Drupal::token()->replace($item->title, array($entity->getEntityTypeId() => $entity), array('sanitize' => FALSE, 'clear' => TRUE)); } + // If the description is available, use it for the link title attribute. + if (empty($settings['url_only']) && !empty($item->description)) { + // Unsanitized token replacement here because $options['html'] is FALSE + // by default in _l(). + $link_description = \Drupal::token()->replace($item->description, [$entity->getEntityTypeId() => $entity], ['sanitize' => FALSE, 'clear' => TRUE]); + } + // Trim the link text to the desired length. if (!empty($settings['trim_length'])) { $link_title = Unicode::truncate($link_title, $settings['trim_length'], FALSE, TRUE); @@ -215,8 +223,8 @@ public function viewElements(FieldItemListInterface $items) { '#type' => 'link', '#title' => $link_title, '#options' => $url->getOptions(), + '#url' => $url, ); - $element[$delta]['#url'] = $url; if (!empty($item->_attributes)) { $element[$delta]['#options'] += array ('attributes' => array()); @@ -225,6 +233,8 @@ public function viewElements(FieldItemListInterface $items) { // formatter output and should not be rendered in the field template. unset($item->_attributes); } + + $element[$delta]['#options']['attributes']['title'] = $link_description; } } diff --git a/core/modules/link/src/Plugin/Field/FieldType/LinkItem.php b/core/modules/link/src/Plugin/Field/FieldType/LinkItem.php index aef95e3..720d0a6 100644 --- a/core/modules/link/src/Plugin/Field/FieldType/LinkItem.php +++ b/core/modules/link/src/Plugin/Field/FieldType/LinkItem.php @@ -120,6 +120,17 @@ public function fieldSettingsForm(array $form, FormStateInterface $form_state) { ), ); + $element['description'] = array( + '#type' => 'radios', + '#title' => t('Allow link description'), + '#default_value' => $this->getSetting('description'), + '#options' => array( + DRUPAL_DISABLED => t('Disabled'), + DRUPAL_OPTIONAL => t('Optional'), + DRUPAL_REQUIRED => t('Required'), + ), + ); + return $element; } @@ -145,6 +156,20 @@ public static function generateSampleValue(FieldDefinitionInterface $field_defin $values['title'] = mt_rand(0,1) ? $random->sentences(4) : ''; break; } + + switch ($field_definition->getSetting('description')) { + case DRUPAL_DISABLED: + $values['description'] = ''; + break; + case DRUPAL_REQUIRED: + $values['description'] = $random->sentences(4); + break; + case DRUPAL_OPTIONAL: + // In case of optional description, randomize its generation. + $values['description'] = mt_rand(0,1) ? $random->sentences(4) : ''; + break; + } + $values['uri'] = 'http://www.' . $random->word($domain_length) . '.' . $tlds[mt_rand(0, (sizeof($tlds)-1))]; return $values; }