class ExtraFieldViewsPlugin in Entity Extra Field 8
Same name and namespace in other branches
- 2.0.x src/Plugin/ExtraFieldType/ExtraFieldViewsPlugin.php \Drupal\entity_extra_field\Plugin\ExtraFieldType\ExtraFieldViewsPlugin
Define extra field views plugin.
Plugin annotation
@ExtraFieldType(
id = "views",
label = @Translation("Views")
)
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\ExtraFieldViewsPlugin
- class \Drupal\entity_extra_field\ExtraFieldTypePluginBase implements ExtraFieldTypePluginInterface uses PluginDependencyTrait
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of ExtraFieldViewsPlugin
File
- src/
Plugin/ ExtraFieldType/ ExtraFieldViewsPlugin.php, line 21
Namespace
Drupal\entity_extra_field\Plugin\ExtraFieldTypeView source
class ExtraFieldViewsPlugin extends ExtraFieldTypePluginBase {
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return [
'display' => NULL,
'view_name' => NULL,
'arguments' => [],
] + parent::defaultConfiguration();
}
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form = parent::buildConfigurationForm($form, $form_state);
$view_name = $this
->getPluginFormStateValue('view_name', $form_state);
$form['view_name'] = [
'#type' => 'select',
'#title' => $this
->t('View'),
'#required' => TRUE,
'#options' => $this
->getViewOptions(),
'#empty_option' => $this
->t('- Select -'),
'#default_value' => $view_name,
'#ajax' => [
'event' => 'change',
'method' => 'replace',
] + $this
->extraFieldPluginAjax(),
];
if (isset($view_name) && !empty($view_name)) {
/** @var \Drupal\views\Entity\View $instance */
$view = $this
->loadView($view_name);
$display = $this
->getPluginFormStateValue('display', $form_state);
$form['display'] = [
'#type' => 'select',
'#title' => $this
->t('Display'),
'#required' => TRUE,
'#options' => $this
->getViewDisplayOptions($view),
'#default_value' => $display,
];
$form['arguments'] = [
'#type' => 'textfield',
'#title' => $this
->t('Arguments'),
'#description' => $this
->t('Input the views display arguments. If there
are multiple, use a comma delimiter. <br/> <strong>Note:</strong>
Tokens are supported.'),
'#default_value' => $this
->getPluginFormStateValue('arguments', $form_state),
];
if ($this->moduleHandler
->moduleExists('token')) {
$form['token_replacements'] = [
'#theme' => 'token_tree_link',
'#token_types' => $this
->getEntityTokenTypes($this
->getTargetEntityTypeId(), $this
->getTargetEntityTypeBundle()
->id()),
];
}
}
return $form;
}
/**
* {@inheritdoc}
*/
public function build(EntityInterface $entity, EntityDisplayInterface $display) {
return $this
->renderView($entity);
}
/**
* {@inheritDoc}
*/
public function calculateDependencies() {
/** @var \Drupal\views\ViewEntityInterface $view */
if ($view = $this
->getView()) {
$this
->addDependencies($view
->getDependencies());
}
return parent::calculateDependencies();
}
/**
* Render the view.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The view entity instance.
*
* @return array|null
* An renderable array of the view.
*
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
*/
protected function renderView(EntityInterface $entity) {
$view_name = $this
->getViewName();
if (!isset($view_name)) {
return [];
}
$view_arguments = $this
->getViewArguments($entity);
return views_embed_view($view_name, $this
->getViewDisplay(), ...$view_arguments);
}
/**
* Get the view instance.
*
* @return bool|\Drupal\Core\Entity\EntityInterface|null
* The view instance.
*
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
*/
protected function getView() {
$view_name = $this
->getViewName();
if (!isset($view_name)) {
return FALSE;
}
return $this
->loadView($view_name);
}
/**
* Get the view name.
*
* @return string|null
* The view name; otherwise NULL.
*/
protected function getViewName() {
return $this
->getConfiguration()['view_name'] ?? NULL;
}
/**
* Get the view display.
*
* @return string
* The view display name; otherwise default.
*/
protected function getViewDisplay() {
$configuration = $this
->getConfiguration();
return isset($configuration['display']) ? $configuration['display'] : 'default';
}
/**
* Get the view arguments.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity instance.
*
* @return array
* An array of view arguments.
*
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
*/
protected function getViewArguments(EntityInterface $entity) {
$configuration = $this
->getConfiguration();
if (!isset($configuration['arguments']) || empty($configuration['arguments'])) {
return [];
}
$arguments = array_filter(explode(',', $configuration['arguments']));
foreach ($arguments as &$argument) {
$argument = $this
->processEntityToken($argument, $entity);
}
return $arguments;
}
/**
* Get view options.
*
* @return array
* An array of view options.
*
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
*/
protected function getViewOptions() {
$options = [];
/** @var \Drupal\views\Entity\View $view */
foreach ($this
->getActiveViewList() as $view_id => $view) {
$options[$view_id] = $view
->label();
}
return $options;
}
/**
* Get view display options.
*
* @param \Drupal\views\Entity\View $view
* The view instance.
*
* @return array
* An array of view display options.
*/
protected function getViewDisplayOptions(View $view) {
$options = [];
$exec = $view
->getExecutable();
$exec
->initHandlers();
/** @var \Drupal\views\Plugin\views\display\DisplayPluginInterface $display */
foreach ($exec->displayHandlers
->getIterator() as $display_id => $display) {
if (!isset($display->display) || !isset($display->display['display_title'])) {
continue;
}
$options[$display_id] = $display->display['display_title'];
}
return $options;
}
/**
* Load view instance.
*
* @param $view_name
* The view name.
*
* @return \Drupal\Core\Entity\EntityInterface|null
* The view instance.
*
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
*/
protected function loadView($view_name) {
return $this
->getViewStorage()
->load($view_name);
}
/**
* Get active view list.
*
* @return \Drupal\Core\Entity\EntityInterface[]
* An array of active views.
*
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
*/
protected function getActiveViewList() {
return $this
->getViewStorage()
->loadByProperties([
'status' => TRUE,
]);
}
/**
* Get view storage instance.
*
* @return \Drupal\Core\Entity\EntityStorageInterface
* The view storage instance.
*
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
*/
protected function getViewStorage() {
return $this->entityTypeManager
->getStorage('view');
}
}
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. | |
ExtraFieldTypePluginBase:: |
protected | property | ||
ExtraFieldTypePluginBase:: |
protected | property | ||
ExtraFieldTypePluginBase:: |
protected | property | ||
ExtraFieldTypePluginBase:: |
protected | property | ||
ExtraFieldTypePluginBase:: |
protected | property | ||
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 |
ExtraFieldViewsPlugin:: |
public | function |
Build the render array of the extra field type contents. Overrides ExtraFieldTypePluginInterface:: |
|
ExtraFieldViewsPlugin:: |
public | function |
Form constructor. Overrides ExtraFieldTypePluginBase:: |
|
ExtraFieldViewsPlugin:: |
public | function |
Calculates dependencies for the configured plugin. Overrides ExtraFieldTypePluginBase:: |
|
ExtraFieldViewsPlugin:: |
public | function |
Gets default configuration for this plugin. Overrides ExtraFieldTypePluginBase:: |
|
ExtraFieldViewsPlugin:: |
protected | function | Get active view list. | |
ExtraFieldViewsPlugin:: |
protected | function | Get the view instance. | |
ExtraFieldViewsPlugin:: |
protected | function | Get the view arguments. | |
ExtraFieldViewsPlugin:: |
protected | function | Get the view display. | |
ExtraFieldViewsPlugin:: |
protected | function | Get view display options. | |
ExtraFieldViewsPlugin:: |
protected | function | Get the view name. | |
ExtraFieldViewsPlugin:: |
protected | function | Get view options. | |
ExtraFieldViewsPlugin:: |
protected | function | Get view storage instance. | |
ExtraFieldViewsPlugin:: |
protected | function | Load view instance. | |
ExtraFieldViewsPlugin:: |
protected | function | Render the view. | |
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. |