public function Link::preRender in Field Group Link 8
Same name and namespace in other branches
- 8.3 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 125
Class
- Link
- Plugin implementation of the 'link' formatter.
Namespace
Drupal\field_group_link\Plugin\field_group\FieldGroupFormatterCode
public function preRender(&$element, $rendering_object) {
if (!isset($this->group->entity_type)) {
return;
}
// Get the entity key from the entity type.
$entity_key = '#' . $this->group->entity_type;
if (!isset($rendering_object[$entity_key])) {
// Some entity types store the key in an arbitrary name.
// Check for the ones that we know of.
switch ($this->group->entity_type) {
case 'taxonomy_term':
$entity_key = '#term';
break;
case 'user':
$entity_key = '#account';
break;
// Otherwise just search for #entity.
default:
$entity_key = '#entity';
}
}
if (isset($rendering_object[$entity_key]) && is_object($rendering_object[$entity_key])) {
$entity = $rendering_object[$entity_key];
}
else {
// 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 = \Drupal::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];
}
}
}