public function Link::preRender in Field Group Link 8.3
Same name and namespace in other branches
- 8 src/Plugin/field_group/FieldGroupFormatter/Link.php \Drupal\field_group_link\Plugin\field_group\FieldGroupFormatter\Link::preRender()
File
- src/
Plugin/ field_group/ FieldGroupFormatter/ Link.php, line 188
Class
- Link
- Plugin implementation of the 'link' formatter.
Namespace
Drupal\field_group_link\Plugin\field_group\FieldGroupFormatterCode
public function preRender(&$element, $rendering_object) {
$entity = $this
->getEntity($rendering_object);
if (!$entity) {
// We can't find the entity.
// There's nothing we can do, so avoid attempting to create a link.
return;
}
$url = NULL;
switch ($this
->getSetting('target')) {
case 'entity':
$url = $entity
->toUrl();
break;
case 'custom_uri':
$uri = $this
->getSetting('custom_uri');
$uri = $this->token
->replace($uri, array(
$this->group->entity_type => $entity,
), array(
'clear' => TRUE,
'sanitize' => TRUE,
));
try {
$url = Url::fromUri($uri);
} catch (\InvalidArgumentException $e) {
return;
}
break;
default:
$url = $this
->getUrlFromField($entity);
}
if ($url) {
$element += array(
'#type' => 'field_group_link',
'#url' => $url,
'#options' => array(
'attributes' => array(
'class' => $this
->getClasses(),
),
),
);
$target_attribute = $this
->getSetting('target_attribute');
if ($target_attribute && $target_attribute != 'default') {
$element['#options']['attributes']['target'] = $target_attribute;
}
if (!empty($this
->getSetting('id'))) {
$element['#options']['attributes']['id'] = $this
->getSetting('id');
}
// Copy each child element into the link title.
// Create a reference in case the content has not yet been generated.
foreach (Element::children($element) as $group_child) {
$element['#title'][$group_child] =& $element[$group_child];
}
}
}