Commit c35607f3 authored by alexpott's avatar alexpott

Issue #2047753 by yched: LinkFormatter::prepareView() is useless.

parent 926a0677
......@@ -9,8 +9,10 @@
use Drupal\field\Annotation\FieldFormatter;
use Drupal\Core\Annotation\Translation;
use Drupal\Component\Utility\Url;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\Field\FieldInterface;
use Drupal\Core\Entity\Field\FieldItemInterface;
use Drupal\field\Plugin\Type\Formatter\FormatterBase;
/**
......@@ -113,40 +115,6 @@ public function settingsSummary() {
return $summary;
}
/**
* {@inheritdoc}
*/
public function prepareView(array $entities, $langcode, array $items) {
$settings = $this->getSettings();
foreach ($entities as $id => $entity) {
foreach ($items[$id] as $delta => $item) {
// Split out the link into the parts required for url(): path and options.
$parsed = drupal_parse_url($item->url);
$item->path = $parsed['path'];
$item->options = array(
'query' => $parsed['query'],
'fragment' => $parsed['fragment'],
'attributes' => $item->attributes,
);
// Add optional 'rel' attribute to link options.
if (!empty($settings['rel'])) {
$options = $item->options;
$options['attributes']['rel'] = $settings['rel'];
$item->options = $options;
}
// Add optional 'target' attribute to link options.
if (!empty($settings['target'])) {
$options = $item->options;
$options['attributes']['target'] = $settings['target'];
$item->options = $options;
}
}
}
}
/**
* {@inheritdoc}
*/
......@@ -176,16 +144,55 @@ public function viewElements(EntityInterface $entity, $langcode, FieldInterface
);
}
else {
$link = $this->buildLink($item);
$element[$delta] = array(
'#type' => 'link',
'#title' => $link_title,
'#href' => $item->path,
'#options' => $item->options,
'#href' => $link['path'],
'#options' => $link['options'],
);
}
}
return $element;
}
/**
* Builds the link information for a link field item.
*
* @param \Drupal\Core\Entity\Field\FieldItemInterface $item
* The link field item being rendered.
*
* @return array
* An array with the following key/value pairs:
* - 'path': a string suitable for the $path parameter in l().
* - 'options': an array suitable for the $options parameter in l().
*/
protected function buildLink(FieldItemInterface $item) {
$settings = $this->getSettings();
// Split out the link into the parts required for url(): path and options.
$parsed_url = Url::parse($item->url);
$result = array(
'path' => $parsed_url['path'],
'options' => array(
'query' => $parsed_url['query'],
'fragment' => $parsed_url['fragment'],
'attributes' => $item->attributes,
),
);
// Add optional 'rel' attribute to link options.
if (!empty($settings['rel'])) {
$result['options']['attributes']['rel'] = $settings['rel'];
}
// Add optional 'target' attribute to link options.
if (!empty($settings['target'])) {
$result['options']['attributes']['target'] = $settings['target'];
}
return $result;
}
}
......@@ -67,12 +67,14 @@ public function viewElements(EntityInterface $entity, $langcode, FieldInterface
$link_title = truncate_utf8($link_title, $settings['trim_length'], FALSE, TRUE);
$url_title = truncate_utf8($item->url, $settings['trim_length'], FALSE, TRUE);
}
$link = $this->buildLink($item);
$element[$delta] = array(
'#theme' => 'link_formatter_link_separate',
'#title' => $link_title,
'#url_title' => $url_title,
'#href' => $item->path,
'#options' => $item->options,
'#href' => $link['path'],
'#options' => $link['options'],
);
}
return $element;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment