View source
<?php
declare (strict_types=1);
namespace Drupal\entity_extra_field\Plugin\ExtraFieldType;
use Drupal\Core\Link;
use Drupal\Core\Utility\Token;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Access\AccessResultInterface;
use Drupal\Core\Access\AccessManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\Display\EntityDisplayInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\BubbleableMetadata;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Url;
use Drupal\entity_extra_field\ExtraFieldTypePluginBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ExtraFieldEntityLinkPlugin extends ExtraFieldTypePluginBase {
protected $accessManager;
public function __construct(array $configuration, string $plugin_id, $plugin_definition, Token $token, ModuleHandlerInterface $module_handler, RouteMatchInterface $current_route_match, EntityTypeManagerInterface $entity_type_manager, EntityFieldManagerInterface $entity_field_manager, AccessManagerInterface $access_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $token, $module_handler, $current_route_match, $entity_type_manager, $entity_field_manager);
$this->accessManager = $access_manager;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('token'), $container
->get('module_handler'), $container
->get('current_route_match'), $container
->get('entity_type.manager'), $container
->get('entity_field.manager'), $container
->get('access_manager'));
}
public function defaultConfiguration() : array {
return [
'link_text' => NULL,
'link_template' => NULL,
'link_target' => NULL,
] + parent::defaultConfiguration();
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) : array {
$form = parent::buildConfigurationForm($form, $form_state);
$configuration = $this
->getConfiguration();
$form['link_template'] = [
'#type' => 'select',
'#title' => $this
->t('Link Template'),
'#require' => TRUE,
'#options' => $this
->getEntityLinkTemplateOptions(),
'#empty_option' => $this
->t('- Select -'),
'#required' => TRUE,
'#default_value' => $configuration['link_template'],
];
$form['link_text'] = [
'#type' => 'textfield',
'#title' => $this
->t('Link Text'),
'#default_value' => $configuration['link_text'],
'#size' => 25,
'#required' => TRUE,
];
$form['link_target'] = [
'#type' => 'select',
'#title' => $this
->t('Link Target'),
'#options' => [
'_blank',
],
'#empty_option' => $this
->t('- Default -'),
'#default_value' => $configuration['link_target'],
];
return $form;
}
public function build(EntityInterface $entity, EntityDisplayInterface $display) : array {
$link = $this
->buildEntityLink($entity);
$accessResult = $this
->urlAccessResult($link
->getUrl());
$build = $accessResult
->isAllowed() ? $link
->toRenderable() : [];
BubbleableMetadata::createFromObject($accessResult)
->applyTo($build);
return $build;
}
public function urlAccessResult(Url $url, AccountInterface $account = NULL) : AccessResultInterface {
if ($url
->isRouted()) {
return $this->accessManager
->checkNamedRoute($url
->getRouteName(), $url
->getRouteParameters(), $account, TRUE);
}
return AccessResult::allowed();
}
protected function buildEntityLink(EntityInterface $entity) : Link {
$configuration = $this
->getConfiguration();
return $entity
->toLink($configuration['link_text'], $configuration['link_template'], $this
->getEntityLinkOptions());
}
protected function getEntityLinkOptions() : array {
$options = [];
$configuration = $this
->getConfiguration();
if ($target = $configuration['link_target']) {
$options['attributes']['target'] = $target;
}
return $options;
}
protected function getEntityLinkTemplateOptions() : array {
$templates = array_keys($this
->getTargetEntityTypeDefinition()
->getLinkTemplates());
return array_combine($templates, $templates);
}
}