From 41df4b91ef1cbdd6ad0400dab03866927c23a441 Mon Sep 17 00:00:00 2001
From: Lucas D Hedding <lucashedding@1463982.no-reply.drupal.org>
Date: Fri, 26 Apr 2013 08:44:49 -0500
Subject: [PATCH] Issue #1801268 by Risse, mgifford | mradcliffe, heddn: Link
 field accessibility needs to be improved.

---
 .../link/Plugin/field/formatter/LinkFormatter.php  |   25 ++++++++---------
 .../field/formatter/LinkSeparateFormatter.php      |   22 +++++++--------
 .../Drupal/link/Plugin/field/widget/LinkWidget.php |   22 +++++++--------
 .../link/lib/Drupal/link/Tests/LinkFieldTest.php   |   28 ++++++++++----------
 core/modules/link/link.install                     |    4 +--
 core/modules/link/link.module                      |   26 +++++++++---------
 6 files changed, 64 insertions(+), 63 deletions(-)

diff --git a/core/modules/link/lib/Drupal/link/Plugin/field/formatter/LinkFormatter.php b/core/modules/link/lib/Drupal/link/Plugin/field/formatter/LinkFormatter.php
index b71f17e..96cc726 100644
--- a/core/modules/link/lib/Drupal/link/Plugin/field/formatter/LinkFormatter.php
+++ b/core/modules/link/lib/Drupal/link/Plugin/field/formatter/LinkFormatter.php
@@ -41,11 +41,11 @@ public function settingsForm(array $form, array &$form_state) {
 
     $elements['trim_length'] = array(
       '#type' => 'number',
-      '#title' => t('Trim link text length'),
+      '#title' => t('Trim link anchor text length'),
       '#field_suffix' => t('characters'),
       '#default_value' => $this->getSetting('trim_length'),
       '#min' => 1,
-      '#description' => t('Leave blank to allow unlimited link text lengths.'),
+      '#description' => t('Leave blank to allow unlimited link anchor text lengths.'),
     );
     $elements['url_only'] = array(
       '#type' => 'checkbox',
@@ -89,10 +89,10 @@ public function settingsSummary() {
     $settings = $this->getSettings();
 
     if (!empty($settings['trim_length'])) {
-      $summary[] = t('Link text trimmed to @limit characters', array('@limit' => $settings['trim_length']));
+      $summary[] = t('Link anchor text trimmed to @limit characters', array('@limit' => $settings['trim_length']));
     }
     else {
-      $summary[] = t('Link text not trimmed');
+      $summary[] = t('Link anchor text not trimmed');
     }
     if ($this->getPluginId() == 'link' && !empty($settings['url_only'])) {
       if (!empty($settings['url_plain'])) {
@@ -150,31 +150,32 @@ public function viewElements(EntityInterface $entity, $langcode, array $items) {
     $settings = $this->getSettings();
 
     foreach ($items as $delta => $item) {
-      // By default use the full URL as the link title.
-      $link_title = $item['url'];
+      // By default use the full URL as the link anchor text.
+      $link_anchor_text = $item['url'];
 
-      // If the title field value is available, use it for the link title.
-      if (empty($settings['url_only']) && !empty($item['title'])) {
+      // If the anchor text field value is available, use it for the link
+      // anchor text.
+      if (empty($settings['url_only']) && !empty($item['anchor_text'])) {
         // Unsanitizied token replacement here because $options['html'] is FALSE
         // by default in theme_link().
-        $link_title = \Drupal::token()->replace($item['title'], array($entity->entityType() => $entity), array('sanitize' => FALSE, 'clear' => TRUE));
+        $link_anchor_text = \Drupal::token()->replace($item['anchor_text'], array($entity->entityType() => $entity), array('sanitize' => FALSE, 'clear' => TRUE));
       }
 
       // Trim the link title to the desired length.
       if (!empty($settings['trim_length'])) {
-        $link_title = truncate_utf8($link_title, $settings['trim_length'], FALSE, TRUE);
+        $link_anchor_text = truncate_utf8($link_title, $settings['trim_length'], FALSE, TRUE);
       }
 
       if (!empty($settings['url_only']) && !empty($settings['url_plain'])) {
         $element[$delta] = array(
           '#type' => 'markup',
-          '#markup' => check_plain($link_title),
+          '#markup' => check_plain($link_anchor_text),
         );
       }
       else {
         $element[$delta] = array(
           '#type' => 'link',
-          '#title' => $link_title,
+          '#title' => $link_anchor_text,
           '#href' => $item['path'],
           '#options' => $item['options'],
         );
diff --git a/core/modules/link/lib/Drupal/link/Plugin/field/formatter/LinkSeparateFormatter.php b/core/modules/link/lib/Drupal/link/Plugin/field/formatter/LinkSeparateFormatter.php
index b2d3882..01d28dd 100644
--- a/core/modules/link/lib/Drupal/link/Plugin/field/formatter/LinkSeparateFormatter.php
+++ b/core/modules/link/lib/Drupal/link/Plugin/field/formatter/LinkSeparateFormatter.php
@@ -23,7 +23,7 @@
  * @Plugin(
  *   id = "link_separate",
  *   module = "link",
- *   label = @Translation("Separate title and URL"),
+ *   label = @Translation("Separate anchor_text and URL"),
  *   field_types = {
  *     "link"
  *   },
@@ -54,22 +54,22 @@ public function viewElements(EntityInterface $entity, $langcode, array $items) {
         $link_title = \Drupal::token()->replace($item['title'], array($entity->entityType() => $entity), array('sanitize' => FALSE, 'clear' => TRUE));
       }
 
-      // The link_separate formatter has two titles; the link title (as in the
-      // field values) and the URL itself. If there is no title value,
-      // $link_title defaults to the URL, so it needs to be unset.
-      // The URL title may need to be trimmed as well.
-      if (empty($item['title'])) {
-        $link_title = NULL;
+      // The link_separate formatter has two anchor texts; the link anchor text (as in the
+      // field values) and the URL itself. If there is no anchor text value,
+      // $link_anchor_text defaults to the URL, so it needs to be unset.
+      // The URL anchor text may need to be trimmed as well.
+      if (empty($item['anchor_text'])) {
+        $link_anchor_text = NULL;
       }
-      $url_title = $item['url'];
+      $url_anchor_text = $item['url'];
       if (!empty($settings['trim_length'])) {
         $link_title = truncate_utf8($link_title, $settings['trim_length'], FALSE, TRUE);
-        $url_title = truncate_utf8($item['url'], $settings['trim_length'], FALSE, TRUE);
+        $url_anchor_text = truncate_utf8($item['url'], $settings['trim_length'], FALSE, TRUE);
       }
       $element[$delta] = array(
         '#theme' => 'link_formatter_link_separate',
-        '#title' => $link_title,
-        '#url_title' => $url_title,
+        '#title' => $link_anchor_text,
+        '#url_anchor_text' => $url_anchor_text,
         '#href' => $item['path'],
         '#options' => $item['options'],
       );
diff --git a/core/modules/link/lib/Drupal/link/Plugin/field/widget/LinkWidget.php b/core/modules/link/lib/Drupal/link/Plugin/field/widget/LinkWidget.php
index 8a1aebb..12bba8c 100644
--- a/core/modules/link/lib/Drupal/link/Plugin/field/widget/LinkWidget.php
+++ b/core/modules/link/lib/Drupal/link/Plugin/field/widget/LinkWidget.php
@@ -43,20 +43,20 @@ public function formElement(array $items, $delta, array $element, $langcode, arr
       '#maxlength' => 2048,
       '#required' => $element['#required'],
     );
-    $element['title'] = array(
+    $element['anchor_text'] = array(
       '#type' => 'textfield',
-      '#title' => t('Title'),
+      '#title' => t('Anchor Text'),
       '#placeholder' => $this->getSetting('placeholder_title'),
-      '#default_value' => isset($items[$delta]['title']) ? $items[$delta]['title'] : NULL,
+      '#default_value' => isset($items[$delta]['anchor_text']) ? $items[$delta]['anchor_text'] : NULL,
       '#maxlength' => 255,
-      '#access' => $instance['settings']['title'] != DRUPAL_DISABLED,
+      '#access' => $settings['anchor_text'] != DRUPAL_DISABLED,
     );
-    // Post-process the title field to make it conditionally required if URL is
+    // Post-process the anchor text 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'));
+    if (!$is_field_edit_form && $instance['settings']['anchor_text'] == DRUPAL_REQUIRED) {
+      $element['#element_validate'] = array(array($this, 'validateAnchorText'));
     }
 
     // Exposing the attributes array in the widget is left for alternate and more
@@ -109,12 +109,12 @@ public function settingsForm(array $form, array &$form_state) {
   /**
    * Form element validation handler for link_field_widget_form().
    *
-   * Conditionally requires the link title if a URL value was filled in.
+   * Conditionally requires the link anchor text if a URL value was filled in.
    */
-  function validateTitle(&$element, &$form_state, $form) {
+  function validateAnchorText(&$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'])));
+      $element['anchor_text']['#required'] = TRUE;
+      form_error($element['anchor_text'], t('!name field is required.', array('!name' => $element['anchor_text']['#title'])));
     }
   }
 }
diff --git a/core/modules/link/lib/Drupal/link/Tests/LinkFieldTest.php b/core/modules/link/lib/Drupal/link/Tests/LinkFieldTest.php
index f28bcc2..e599fea 100644
--- a/core/modules/link/lib/Drupal/link/Tests/LinkFieldTest.php
+++ b/core/modules/link/lib/Drupal/link/Tests/LinkFieldTest.php
@@ -54,7 +54,7 @@ function testURLValidation() {
       'entity_type' => 'test_entity',
       'bundle' => 'test_bundle',
       'settings' => array(
-        'title' => DRUPAL_DISABLED,
+        'anchor_text' => DRUPAL_DISABLED,
       ),
       'widget' => array(
         'type' => 'link_default',
@@ -108,9 +108,9 @@ function testURLValidation() {
   }
 
   /**
-   * Tests the title settings of a link field.
+   * Tests the anchor text settings of a link field.
    */
-  function testLinkTitle() {
+  function testLinkAnchorText() {
     // Create a field with settings to validate.
     $this->field = array(
       'field_name' => drupal_strtolower($this->randomName()),
@@ -374,7 +374,7 @@ function testLinkSeparateFormatter() {
       'entity_type' => 'test_entity',
       'bundle' => 'test_bundle',
       'settings' => array(
-        'title' => DRUPAL_OPTIONAL,
+        'anchor_text' => DRUPAL_OPTIONAL,
       ),
       'widget' => array(
         'type' => 'link_default',
@@ -393,18 +393,18 @@ function testLinkSeparateFormatter() {
 
     // Create an entity with two link field values:
     // - The first field item uses a URL only.
-    // - The second field item uses a URL and title.
-    // For consistency in assertion code below, the URL is assigned to the title
+    // - The second field item uses a URL and anchor text.
+    // For consistency in assertion code below, the URL is assigned to the anchor text
     // variable for the first field.
     $this->drupalGet('test-entity/add/test_bundle');
     $url1 = 'http://www.example.com/content/articles/archive?author=John&year=2012#com';
     $url2 = 'http://www.example.org/content/articles/archive?author=John&year=2012#org';
     // Intentionally contains an ampersand that needs sanitization on output.
-    $title2 = 'A very long & strange example title that could break the nice layout of the site';
+    $anchor_text2 = 'A very long & strange example anchor text that could break the nice layout of the site';
     $edit = array(
       "{$this->field['field_name']}[$langcode][0][url]" => $url1,
       "{$this->field['field_name']}[$langcode][1][url]" => $url2,
-      "{$this->field['field_name']}[$langcode][1][title]" => $title2,
+      "{$this->field['field_name']}[$langcode][1][anchor_text]" => $anchor_text2,
     );
     $this->drupalPost(NULL, $edit, t('Save'));
     preg_match('|test-entity/manage/(\d+)/edit|', $this->url, $match);
@@ -429,18 +429,18 @@ function testLinkSeparateFormatter() {
         switch ($setting) {
           case 'trim_length':
             $url = $url1;
-            $url_title = isset($new_value) ? truncate_utf8($url, $new_value, FALSE, TRUE) : $url;
+            $url_anchor_text = isset($new_value) ? truncate_utf8($url, $new_value, FALSE, TRUE) : $url;
             $expected = '<div class="link-item">';
-            $expected .= '<div class="link-url"><a href="' . check_plain($url) . '">' . check_plain($url_title) . '</a></div>';
+            $expected .= '<div class="link-url"><a href="' . check_plain($url) . '">' . check_plain($url_anchor_text) . '</a></div>';
             $expected .= '</div>';
             $this->assertRaw($expected);
 
             $url = $url2;
-            $url_title = isset($new_value) ? truncate_utf8($url, $new_value, FALSE, TRUE) : $url;
-            $title = isset($new_value) ? truncate_utf8($title2, $new_value, FALSE, TRUE) : $title2;
+            $url_anchor_text = isset($new_value) ? truncate_utf8($url, $new_value, FALSE, TRUE) : $url;
+            $anchor_text = isset($new_value) ? truncate_utf8($anchor_text2, $new_value, FALSE, TRUE) : $anchor_text2;
             $expected = '<div class="link-item">';
-            $expected .= '<div class="link-title">' . check_plain($title) . '</div>';
-            $expected .= '<div class="link-url"><a href="' . check_plain($url) . '">' . check_plain($url_title) . '</a></div>';
+            $expected .= '<div class="link-anchor_text">' . check_plain($anchor_text) . '</div>';
+            $expected .= '<div class="link-url"><a href="' . check_plain($url) . '">' . check_plain($url_anchor_text) . '</a></div>';
             $expected .= '</div>';
             $this->assertRaw($expected);
             break;
diff --git a/core/modules/link/link.install b/core/modules/link/link.install
index 3714d35..73d1aef 100644
--- a/core/modules/link/link.install
+++ b/core/modules/link/link.install
@@ -15,8 +15,8 @@ function link_field_schema($field) {
     'length' => 2048,
     'not null' => FALSE,
   );
-  $schema['columns']['title'] = array(
-    'description' => 'The link text.',
+  $schema['columns']['anchor_text'] = array(
+    'description' => 'The link anchor text.',
     'type' => 'varchar',
     'length' => 255,
     'not null' => FALSE,
diff --git a/core/modules/link/link.module b/core/modules/link/link.module
index 9f0fa1e..afaf722 100644
--- a/core/modules/link/link.module
+++ b/core/modules/link/link.module
@@ -15,7 +15,7 @@ function link_help($path, $arg) {
     case 'admin/help#link':
       $output = '';
       $output .= '<h3>' . t('About') . '</h3>';
-      $output .= '<p>' . t('The Link module defines a simple link field type for the Field module. Links are external URLs, can have an optional title for each link, and they can be formatted when displayed. See the <a href="@field-help">Field module help page</a> for more information about fields.', array('@field-help' => url('admin/help/field'))) . '</p>';
+      $output .= '<p>' . t('The Link module defines a simple link field type for the Field module. Links are external URLs, can have an optional anchor text for each link, and they can be formatted when displayed. See the <a href="@field-help">Field module help page</a> for more information about fields.', array('@field-help' => url('admin/help/field'))) . '</p>';
       return $output;
   }
 }
@@ -26,9 +26,9 @@ function link_help($path, $arg) {
 function link_field_info() {
   $types['link'] = array(
     'label' => t('Link'),
-    'description' => t('Stores a URL string, optional varchar title, and optional blob of attributes to assemble a link.'),
+    'description' => t('Stores a URL string, optional varchar anchor text, and optional blob of attributes to assemble a link.'),
     'instance_settings' => array(
-      'title' => DRUPAL_OPTIONAL,
+      'anchor_text' => DRUPAL_OPTIONAL,
     ),
     'default_widget' => 'link_default',
     'default_formatter' => 'link',
@@ -41,10 +41,10 @@ function link_field_info() {
  * Implements hook_field_instance_settings_form().
  */
 function link_field_instance_settings_form($field, $instance) {
-  $form['title'] = array(
+  $form['anchor_text'] = array(
     '#type' => 'radios',
-    '#title' => t('Allow link title'),
-    '#default_value' => isset($instance['settings']['title']) ? $instance['settings']['title'] : DRUPAL_OPTIONAL,
+    '#title' => t('Allow link anchor text'),
+    '#default_value' => isset($instance['settings']['anchor_text']) ? $instance['settings']['anchor_text'] : DRUPAL_OPTIONAL,
     '#options' => array(
       DRUPAL_DISABLED => t('Disabled'),
       DRUPAL_OPTIONAL => t('Optional'),
@@ -84,9 +84,9 @@ function link_field_is_empty($item, $field) {
  */
 function link_field_presave(EntityInterface $entity, $field, $instance, $langcode, &$items) {
   foreach ($items as $delta => &$item) {
-    // Trim any spaces around the URL and title.
+    // Trim any spaces around the URL and anchor text.
     $item['url'] = trim($item['url']);
-    $item['title'] = trim($item['title']);
+    $item['anchor_text'] = trim($item['anchor_text']);
 
     // Serialize the attributes array.
     $item['attributes'] = !empty($item['attributes']) ? serialize($item['attributes']) : NULL;
@@ -99,21 +99,21 @@ function link_field_presave(EntityInterface $entity, $field, $instance, $langcod
 function link_theme() {
   return array(
     'link_formatter_link_separate' => array(
-      'variables' => array('title' => NULL, 'url_title' => NULL, 'href' => NULL, 'options' => array()),
+      'variables' => array('anchor_text' => NULL, 'url_anchor_text' => NULL, 'href' => NULL, 'options' => array()),
     ),
   );
 }
 
 /**
- * Formats a link as separate title and URL elements.
+ * Formats a link as separate anchor_text and URL elements.
  */
 function theme_link_formatter_link_separate($vars) {
   $output = '';
   $output .= '<div class="link-item">';
-  if (!empty($vars['title'])) {
-    $output .= '<div class="link-title">' . check_plain($vars['title']) . '</div>';
+  if (!empty($vars['anchor_text'])) {
+    $output .= '<div class="link-anchor-text">' . check_plain($vars['anchor_text']) . '</div>';
   }
-  $output .= '<div class="link-url">' . l($vars['url_title'], $vars['href'], $vars['options']) . '</div>';
+  $output .= '<div class="link-url">' . l($vars['url_anchor_text'], $vars['href'], $vars['options']) . '</div>';
   $output .= '</div>';
   return $output;
 }
-- 
1.7.10.4

