reverted: --- b/core/modules/field/modules/link/lib/Drupal/link/Tests/LinkFieldTest.php +++ a/core/modules/field/modules/link/lib/Drupal/link/Tests/LinkFieldTest.php @@ -54,7 +54,7 @@ 'entity_type' => 'test_entity', 'bundle' => 'test_bundle', 'settings' => array( + 'title' => DRUPAL_DISABLED, - 'anchor_text' => DRUPAL_DISABLED, ), 'widget' => array( 'type' => 'link_default', @@ -107,9 +107,9 @@ } /** + * 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()), @@ -121,7 +121,7 @@ 'entity_type' => 'test_entity', 'bundle' => 'test_bundle', 'settings' => array( + 'title' => DRUPAL_OPTIONAL, - 'anchor_text' => DRUPAL_OPTIONAL, ), 'widget' => array( 'type' => 'link_default', @@ -140,10 +140,10 @@ field_create_instance($this->instance); $langcode = LANGUAGE_NOT_SPECIFIED; + // Verify that the title field works according to the field setting. + foreach (array(DRUPAL_DISABLED, DRUPAL_REQUIRED, DRUPAL_OPTIONAL) as $title_setting) { + // Update the title field setting. + $this->instance['settings']['title'] = $title_setting; - // Verify that the anchor_text field works according to the field setting. - foreach (array(DRUPAL_DISABLED, DRUPAL_REQUIRED, DRUPAL_OPTIONAL) as $anchor_text_setting) { - // Update the anchor text field setting. - $this->instance['settings']['anchor_text'] = $anchor_text_setting; field_update_instance($this->instance); // Display creation form. @@ -151,43 +151,45 @@ $this->assertFieldByName("{$this->field['field_name']}[$langcode][0][url]", '', 'URL field found.'); $this->assertRaw('placeholder="http://example.com"'); + if ($title_setting === DRUPAL_DISABLED) { + $this->assertNoFieldByName("{$this->field['field_name']}[$langcode][0][title]", '', 'Title field not found.'); - if ($anchor_text_setting === DRUPAL_DISABLED) { - $this->assertNoFieldByName("{$this->field['field_name']}[$langcode][0][anchor_text]", '', 'anchor text field not found.'); $this->assertNoRaw('placeholder="Enter a title for this link"'); } else { + $this->assertRaw('placeholder="Enter a title for this link"'); + - $this->assertFieldByName("{$this->field['field_name']}[$langcode][0][anchor_text]", '', 'anchor text field found.'); $this->assertFieldByName("{$this->field['field_name']}[$langcode][0][title]", '', 'Title field found.'); + if ($title_setting === DRUPAL_REQUIRED) { + // Verify that the title is required, if the URL is non-empty. - if ($anchor_text_setting === DRUPAL_REQUIRED) { - // Verify that the anchor text is required, if the URL is non-empty. $edit = array( "{$this->field['field_name']}[$langcode][0][url]" => 'http://www.example.com', ); $this->drupalPost(NULL, $edit, t('Save')); + $this->assertText(t('!name field is required.', array('!name' => t('Title')))); + + // Verify that the title is not required, if the URL is empty. - $this->assertText(t('!name field is required.', array('!name' => t('anchor_text')))); - - // Verify that the anchor text is not required, if the URL is empty. $edit = array( "{$this->field['field_name']}[$langcode][0][url]" => '', ); $this->drupalPost(NULL, $edit, t('Save')); + $this->assertNoText(t('!name field is required.', array('!name' => t('Title')))); + + // Verify that a URL and title meets requirements. - $this->assertNoText(t('!name field is required.', array('!name' => t('anchor_text')))); - // Verify that a URL and anchor text meets requirements. $this->drupalGet('test-entity/add/test_bundle'); $edit = array( "{$this->field['field_name']}[$langcode][0][url]" => 'http://www.example.com', + "{$this->field['field_name']}[$langcode][0][title]" => 'Example', - "{$this->field['field_name']}[$langcode][0][anchor_text]" => 'Example', ); $this->drupalPost(NULL, $edit, t('Save')); + $this->assertNoText(t('!name field is required.', array('!name' => t('Title')))); - $this->assertNoText(t('!name field is required.', array('!name' => t('anchor_text')))); } } } + // Verify that a link without title is rendered using the URL as link text. - // Verify that a link without anchor text is rendered using the URL as link text. $value = 'http://www.example.com/'; $edit = array( + "{$this->field['field_name']}[$langcode][0][url]" => $value, - "{$this->field['field_name']}[$langcode][0][anchor_text]" => '', "{$this->field['field_name']}[$langcode][0][title]" => '', ); $this->drupalPost(NULL, $edit, t('Save')); @@ -199,16 +201,16 @@ $expected_link = l($value, $value); $this->assertRaw($expected_link); + // Verify that a link with title is rendered using the title as link text. + $title = $this->randomName(); - // Verify that a link with anchor text is rendered using the anchor text as link text. - $anchor_text = $this->randomName(); $edit = array( + "{$this->field['field_name']}[$langcode][0][title]" => $title, - "{$this->field['field_name']}[$langcode][0][anchor_text]" => $anchor_text, ); $this->drupalPost("test-entity/manage/$id/edit", $edit, t('Save')); $this->assertRaw(t('test_entity @id has been updated.', array('@id' => $id))); $this->renderTestEntity($id); + $expected_link = l($title, $value); - $expected_link = l($anchor_text, $value); $this->assertRaw($expected_link); } @@ -228,7 +230,7 @@ 'entity_type' => 'test_entity', 'bundle' => 'test_bundle', 'settings' => array( + 'title' => DRUPAL_OPTIONAL, - 'anchor_text' => DRUPAL_OPTIONAL, ), 'widget' => array( 'type' => 'link_default', @@ -245,21 +247,21 @@ // 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'; + $title1 = $url1; - $anchor_text1 = $url1; // 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, + // Note that $title1 is not submitted. + "{$this->field['field_name']}[$langcode][0][title]" => '', - // Note that $anchor_text1 is not submitted. - "{$this->field['field_name']}[$langcode][0][anchor_text]" => '', "{$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); @@ -297,31 +299,31 @@ switch ($setting) { case 'trim_length': $url = $url1; + $title = isset($new_value) ? truncate_utf8($title1, $new_value, FALSE, TRUE) : $title1; + $this->assertRaw('' . check_plain($title) . ''); - $anchor_text = isset($new_value) ? truncate_utf8($anchor_text1, $new_value, FALSE, TRUE) : $anchor_text1; - $this->assertRaw('' . check_plain($anchor_text) . ''); $url = $url2; + $title = isset($new_value) ? truncate_utf8($title2, $new_value, FALSE, TRUE) : $title2; + $this->assertRaw('' . check_plain($title) . ''); - $anchor_text = isset($new_value) ? truncate_utf8($anchor_text2, $new_value, FALSE, TRUE) : $anchor_text2; - $this->assertRaw('' . check_plain($anchor_text) . ''); break; case 'rel': $rel = isset($new_value) ? ' rel="' . $new_value . '"' : ''; + $this->assertRaw('' . check_plain($title1) . ''); + $this->assertRaw('' . check_plain($title2) . ''); - $this->assertRaw('' . check_plain($anchor_text1) . ''); - $this->assertRaw('' . check_plain($anchor_text2) . ''); break; case 'target': $target = isset($new_value) ? ' target="' . $new_value . '"' : ''; + $this->assertRaw('' . check_plain($title1) . ''); + $this->assertRaw('' . check_plain($title2) . ''); - $this->assertRaw('' . check_plain($anchor_text1) . ''); - $this->assertRaw('' . check_plain($anchor_text2) . ''); break; case 'url_only': // In this case, $new_value is an array. if (!$new_value['url_only']) { + $this->assertRaw('' . check_plain($title1) . ''); + $this->assertRaw('' . check_plain($title2) . ''); - $this->assertRaw('' . check_plain($anchor_text1) . ''); - $this->assertRaw('' . check_plain($anchor_text2) . ''); } else { if (empty($new_value['url_plain'])) { @@ -360,7 +362,7 @@ 'entity_type' => 'test_entity', 'bundle' => 'test_bundle', 'settings' => array( + 'title' => DRUPAL_OPTIONAL, - 'anchor_text' => DRUPAL_OPTIONAL, ), 'widget' => array( 'type' => 'link_default', @@ -377,18 +379,18 @@ // 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); @@ -411,18 +413,18 @@ 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 = ''; $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 = ''; $this->assertRaw($expected); break; reverted: --- b/core/modules/field/modules/link/link.install +++ a/core/modules/field/modules/link/link.install @@ -15,8 +15,8 @@ '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, reverted: --- b/core/modules/field/modules/link/link.module +++ a/core/modules/field/modules/link/link.module @@ -13,7 +13,7 @@ case 'admin/help#link': $output = ''; $output .= '

' . t('About') . '

'; + $output .= '

' . 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 Field module help page for more information about fields.', array('@field-help' => url('admin/help/field'))) . '

'; - $output .= '

' . 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 Field module help page for more information about fields.', array('@field-help' => url('admin/help/field'))) . '

'; return $output; } } @@ -24,9 +24,9 @@ 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', @@ -38,10 +38,10 @@ * 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'), @@ -81,9 +81,9 @@ */ function link_field_presave($entity_type, $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; @@ -148,20 +148,20 @@ '#maxlength' => 2048, '#required' => $element['#required'], ); + $element['title'] = array( - $element['anchor_text'] = array( '#type' => 'textfield', + '#title' => t('Title'), '#placeholder' => isset($widget_settings['placeholder_title']) ? $widget_settings['placeholder_title'] : '', + '#default_value' => isset($items[$delta]['title']) ? $items[$delta]['title'] : NULL, - '#title' => t('Anchor text'), - '#default_value' => isset($items[$delta]['anchor_text']) ? $items[$delta]['anchor_text'] : NULL, '#maxlength' => 255, + '#access' => $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 && $settings['title'] == DRUPAL_REQUIRED) { + $element['#element_validate'][] = 'link_field_widget_validate_title'; - if (!$is_field_edit_form && $settings['anchor_text'] == DRUPAL_REQUIRED) { - $element['#element_validate'][] = 'link_field_widget_validate_anchor_text'; } // Exposing the attributes array in the widget is left for alternate and more @@ -179,12 +179,12 @@ /** * 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 link_field_widget_validate_title(&$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']))); -function link_field_widget_validate_anchor_text(&$element, &$form_state, $form) { - if ($element['url']['#value'] !== '' && $element['anchor_text']['#value'] === '') { - $element['anchor_text']['#required'] = TRUE; - form_error($element['anchor_text'], t('!name field is required.', array('!name' => $element['anchor_text']['#title']))); } } @@ -224,7 +224,7 @@ // @todo Merge into 'link' formatter once there is a #type like 'item' that // can render a compound label and content outside of a form context. $formatters['link_separate'] = array( + 'label' => t('Separate title and URL'), - 'label' => t('Separate anchor text and URL'), 'field types' => array('link'), 'settings' => array( 'trim_length' => 80, @@ -244,11 +244,11 @@ $element['trim_length'] = array( '#type' => 'number', + '#title' => t('Trim link text length'), - '#title' => t('Trim link anchor text length'), '#field_suffix' => t('characters'), '#default_value' => $settings['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.'), ); $element['url_only'] = array( '#type' => 'checkbox', @@ -293,10 +293,10 @@ $summary = array(); 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 (!empty($settings['url_only'])) { if (!empty($settings['url_plain'])) { @@ -344,53 +344,53 @@ $settings = $display['settings']; 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 = token_replace($item['title'], array($entity_type => $entity), array('sanitize' => FALSE, 'clear' => TRUE)); - $link_anchor_text = token_replace($item['anchor_text'], array($entity_type => $entity), array('sanitize' => FALSE, 'clear' => TRUE)); } + // Trim the link title to the desired length. - // Trim the link anchor text 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_anchor_text, $settings['trim_length'], FALSE, TRUE); } if ($display['type'] == 'link') { 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'], ); } } elseif ($display['type'] == 'link_separate') { + // 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'])) { + $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'], ); @@ -405,21 +405,21 @@ 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 .= ''; return $output; } only in patch2: unchanged: --- 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'], ); only in patch2: unchanged: --- 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'], ); only in patch2: unchanged: --- 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']))); } } } only in patch2: unchanged: --- 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 = ''; $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 = ''; $this->assertRaw($expected); break; only in patch2: unchanged: --- 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, only in patch2: unchanged: --- 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 .= '

' . t('About') . '

'; - $output .= '

' . 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 Field module help page for more information about fields.', array('@field-help' => url('admin/help/field'))) . '

'; + $output .= '

' . 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 Field module help page for more information about fields.', array('@field-help' => url('admin/help/field'))) . '

'; 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 .= ''; return $output; }