class ExtraFieldEntityLinkPlugin in Entity Extra Field 8
Same name and namespace in other branches
- 2.0.x src/Plugin/ExtraFieldType/ExtraFieldEntityLinkPlugin.php \Drupal\entity_extra_field\Plugin\ExtraFieldType\ExtraFieldEntityLinkPlugin
Define the extra field entity link type.
Plugin annotation
@ExtraFieldType(
id = "entity_link",
label = @Translation("Entity link")
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\entity_extra_field\ExtraFieldTypePluginBase implements ExtraFieldTypePluginInterface uses PluginDependencyTrait
- class \Drupal\entity_extra_field\Plugin\ExtraFieldType\ExtraFieldEntityLinkPlugin
- class \Drupal\entity_extra_field\ExtraFieldTypePluginBase implements ExtraFieldTypePluginInterface uses PluginDependencyTrait
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of ExtraFieldEntityLinkPlugin
File
- src/
Plugin/ ExtraFieldType/ ExtraFieldEntityLinkPlugin.php, line 22
Namespace
Drupal\entity_extra_field\Plugin\ExtraFieldTypeView source
class ExtraFieldEntityLinkPlugin extends ExtraFieldTypePluginBase {
/**
* {@inheritDoc}
*/
public function defaultConfiguration() {
return [
'link_text' => NULL,
'link_template' => NULL,
'link_target' => NULL,
] + parent::defaultConfiguration();
}
/**
* {@inheritDoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$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;
}
/**
* Build the render array of the extra field type contents.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity type the extra field is being attached too.
* @param \Drupal\Core\Entity\Display\EntityDisplayInterface $display
* The entity display the extra field is apart of.
*
* @return array
* The extra field renderable array.
*
* @throws \Drupal\Core\Entity\EntityMalformedException
*/
public function build(EntityInterface $entity, EntityDisplayInterface $display) {
$link = $this
->buildEntityLink($entity);
// Link and Url seem not to have convenience methods for access including
// cacheability. So inlining a variant of \Drupal\Core\Url::access
$accessResult = $this
->urlAccessResult($link
->getUrl());
$build = $accessResult
->isAllowed() ? $link
->toRenderable() : [];
BubbleableMetadata::createFromObject($accessResult)
->applyTo($build);
return $build;
}
/**
* A copy of \Drupal\Core\Url::access that returns cacheability.
*
* @param \Drupal\Core\Url $url
* The url.
* @param \Drupal\Core\Session\AccountInterface $account
* (optional) Run access checks for this account. Defaults to the current
* user.
*
* @return \Drupal\Core\Access\AccessResultInterface Returns url access result object.
* Returns url access result object.
*/
public function urlAccessResult(Url $url, AccountInterface $account = NULL) {
if ($url
->isRouted()) {
/** @var \Drupal\Core\Access\AccessManagerInterface $accessManager */
$accessManager = \Drupal::service('access_manager');
return $accessManager
->checkNamedRoute($url
->getRouteName(), $url
->getRouteParameters(), $account, TRUE);
}
return AccessResult::allowed();
}
/**
* Build the entity link.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity instance.
*
* @return \Drupal\Core\Link
* The entity link instance.
*
* @throws \Drupal\Core\Entity\EntityMalformedException
*/
protected function buildEntityLink(EntityInterface $entity) {
$configuration = $this
->getConfiguration();
return $entity
->toLink($configuration['link_text'], $configuration['link_template'], $this
->getEntityLinkOptions());
}
/**
* Get the entity link options.
*
* @return array
* An array of the link options.
*/
protected function getEntityLinkOptions() {
$options = [];
$configuration = $this
->getConfiguration();
if ($target = $configuration['link_target']) {
$options['attributes']['target'] = $target;
}
return $options;
}
/**
* Get entity link template options.
*
* @return array
* An array of the entity template options.
*
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
*/
protected function getEntityLinkTemplateOptions() {
$templates = array_keys($this
->getTargetEntityTypeDefinition()
->getLinkTemplates());
return array_combine($templates, $templates);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
DependencyTrait:: |
protected | property | The object's dependencies. | |
DependencyTrait:: |
protected | function | Adds multiple dependencies. | |
DependencyTrait:: |
protected | function | Adds a dependency. | |
ExtraFieldEntityLinkPlugin:: |
public | function |
Build the render array of the extra field type contents. Overrides ExtraFieldTypePluginInterface:: |
|
ExtraFieldEntityLinkPlugin:: |
public | function |
Form constructor. Overrides ExtraFieldTypePluginBase:: |
|
ExtraFieldEntityLinkPlugin:: |
protected | function | Build the entity link. | |
ExtraFieldEntityLinkPlugin:: |
public | function |
Gets default configuration for this plugin. Overrides ExtraFieldTypePluginBase:: |
|
ExtraFieldEntityLinkPlugin:: |
protected | function | Get the entity link options. | |
ExtraFieldEntityLinkPlugin:: |
protected | function | Get entity link template options. | |
ExtraFieldEntityLinkPlugin:: |
public | function | A copy of \Drupal\Core\Url::access that returns cacheability. | |
ExtraFieldTypePluginBase:: |
protected | property | ||
ExtraFieldTypePluginBase:: |
protected | property | ||
ExtraFieldTypePluginBase:: |
protected | property | ||
ExtraFieldTypePluginBase:: |
protected | property | ||
ExtraFieldTypePluginBase:: |
protected | property | ||
ExtraFieldTypePluginBase:: |
public | function |
Calculates dependencies for the configured plugin. Overrides DependentPluginInterface:: |
2 |
ExtraFieldTypePluginBase:: |
public static | function |
Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface:: |
1 |
ExtraFieldTypePluginBase:: |
protected | function | Get extra field plugin ajax properties. | |
ExtraFieldTypePluginBase:: |
public | function | Get extra field plugin ajax. | |
ExtraFieldTypePluginBase:: |
public | function |
Gets this plugin's configuration. Overrides ConfigurableInterface:: |
|
ExtraFieldTypePluginBase:: |
protected | function | Get entity field reference types. | |
ExtraFieldTypePluginBase:: |
protected | function | Get entity token data. | |
ExtraFieldTypePluginBase:: |
protected | function | Get entity token types. | |
ExtraFieldTypePluginBase:: |
protected | function | Get plugin form state value. | |
ExtraFieldTypePluginBase:: |
protected | function | Get target entity type bundle. | |
ExtraFieldTypePluginBase:: |
protected | function | Get target entity type definition. | |
ExtraFieldTypePluginBase:: |
protected | function | Get target entity type identifier. | |
ExtraFieldTypePluginBase:: |
public | function |
Display the extra field plugin label. Overrides ExtraFieldTypePluginInterface:: |
|
ExtraFieldTypePluginBase:: |
protected | function | Process the entity token text. | |
ExtraFieldTypePluginBase:: |
public | function |
Sets the configuration for this plugin instance. Overrides ConfigurableInterface:: |
|
ExtraFieldTypePluginBase:: |
public | function |
Form submission handler. Overrides PluginFormInterface:: |
1 |
ExtraFieldTypePluginBase:: |
public | function |
Form validation handler. Overrides PluginFormInterface:: |
1 |
ExtraFieldTypePluginBase:: |
public | function |
Extra field type view constructor. Overrides PluginBase:: |
1 |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
PluginDependencyTrait:: |
protected | function | Calculates and adds dependencies of a specific plugin instance. | 1 |
PluginDependencyTrait:: |
protected | function | Calculates and returns dependencies of a specific plugin instance. | |
PluginDependencyTrait:: |
protected | function | Wraps the module handler. | 1 |
PluginDependencyTrait:: |
protected | function | Wraps the theme handler. | 1 |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |