EntityReferenceLabelFormatter.php in Entity reference 8
File
lib/Drupal/entityreference/Plugin/field/formatter/EntityReferenceLabelFormatter.php
View source
<?php
namespace Drupal\entityreference\Plugin\field\formatter;
use Drupal\Core\Annotation\Plugin;
use Drupal\Core\Annotation\Translation;
use Drupal\Core\Entity\EntityInterface;
use Drupal\entityreference\Plugin\field\formatter\EntityReferenceFormatterBase;
class EntityReferenceLabelFormatter extends EntityReferenceFormatterBase {
public function settingsForm(array $form, array &$form_state) {
$elements['link'] = array(
'#title' => t('Link label to the referenced entity'),
'#type' => 'checkbox',
'#default_value' => $this
->getSetting('link'),
);
return $elements;
}
public function settingsSummary() {
$summary = array();
$summary[] = $this
->getSetting('link') ? t('Link to the referenced entity') : t('No link');
return implode('<br />', $summary);
}
public function viewElements(EntityInterface $entity, $langcode, array $items) {
parent::viewElements($entity, $langcode, $items);
$instance = $this->instance;
$field = $this->field;
$elements = array();
foreach ($items as $delta => $item) {
$entity = $item['entity'];
$label = $entity
->label();
if ($this
->getSetting('link') && ($uri = $entity
->uri())) {
$elements[$delta] = array(
'#markup' => l($label, $uri['path'], $uri['options']),
);
}
else {
$elements[$delta] = array(
'#markup' => check_plain($label),
);
}
}
return $elements;
}
}