Skip to content
Snippets Groups Projects
Commit 6e2eca87 authored by Jürgen Haas's avatar Jürgen Haas
Browse files

Issue #3413762 by jurgenhaas, jrochate: Set Entity View Mode

parent 528a6821
Branches
Tags
1 merge request!409Issue #3413762: Set Entity View Mode
Pipeline #157234 failed
......@@ -167,6 +167,13 @@ function eca_content_entity_field_values_init(FieldableEntityInterface $entity):
_eca_content_hook_handler()->fieldValuesInit($entity);
}
/**
* Implements hook_entity_view_mode_alter().
*/
function eca_content_entity_view_mode_alter(string &$view_mode, EntityInterface $entity): void {
_eca_content_hook_handler()->viewModeAlter($view_mode, $entity);
}
/**
* Implements hook_ENTITY_TYPE_insert() for field_config entities.
*
......
......@@ -160,6 +160,15 @@ final class ContentEntityEvents {
*/
public const VIEW = 'eca.content_entity.view';
/**
* Identifies \Drupal\eca_content\Event\ContentEntityView event.
*
* @Event
*
* @var string
*/
public const VIEWMODEALTER = 'eca.content_entity.view_mode_alter';
/**
* Identifies \Drupal\eca_content\Event\ContentEntityPrepareView event.
*
......
<?php
namespace Drupal\eca_content\Event;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\eca\Service\ContentEntityTypes;
/**
* Provides an event when the view mode of a content entity can be altered.
*
* @internal
* This class is not meant to be used as a public API. It is subject for name
* change or may be removed completely, also on minor version updates.
*
* @package Drupal\eca_content\Event
*/
class ContentEntityViewModeAlter extends ContentEntityBaseContentEntity {
/**
* The view mode.
*
* @var string
*/
protected string $viewMode;
/**
* Constructor.
*
* @param \Drupal\Core\Entity\ContentEntityInterface $entity
* The entity.
* @param \Drupal\eca\Service\ContentEntityTypes $entity_types
* The entity types.
* @param string $view_mode
* The view mode.
*/
public function __construct(ContentEntityInterface $entity, ContentEntityTypes $entity_types, string $view_mode) {
parent::__construct($entity, $entity_types);
$this->viewMode = $view_mode;
}
/**
* Gets the view mode.
*
* @return string
* The view mode.
*/
public function getViewMode(): string {
return $this->viewMode;
}
/**
* Sets the view mode.
*
* @param string $viewMode
* The view mode.
*/
public function setViewMode(string $viewMode): void {
$this->viewMode = $viewMode;
}
}
......@@ -316,4 +316,20 @@ class HookHandler extends BaseHookHandler {
}
}
/**
* Dispatches event view mode alter.
*
* @param string $view_mode
* The view_mode that is to be used to display the entity.
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity that is being viewed.
*/
public function viewModeAlter(string &$view_mode, EntityInterface $entity): void {
if ($entity instanceof ContentEntityInterface) {
/** @var \Drupal\eca_content\Event\ContentEntityViewModeAlter $event */
$event = $this->triggerEvent->dispatchFromPlugin('content_entity:viewmodealter', $entity, $this->entityTypes, $view_mode);
$view_mode = $event->getViewMode();
}
}
}
<?php
namespace Drupal\eca_content\Plugin\Action;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\eca\Plugin\Action\ConfigurableActionBase;
use Drupal\eca_content\Event\ContentEntityViewModeAlter;
/**
* Flag the entity for creating a new revision.
*
* @Action(
* id = "eca_set_view_mode",
* label = @Translation("Entity: set view mode"),
* description = @Translation("Changes the view mode of the entity. Only work after the event 'Alter entity view mode'."),
* eca_version_introduced = "2.0.0"
* )
*/
class SetViewMode extends ConfigurableActionBase {
/**
* {@inheritdoc}
*/
public function defaultConfiguration(): array {
return [
'new_view_mode' => '',
] + parent::defaultConfiguration();
}
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state): array {
$form = parent::buildConfigurationForm($form, $form_state);
$form['new_view_mode'] = [
'#type' => 'textfield',
'#title' => $this->t('View mode'),
'#default_value' => $this->configuration['new_view_mode'],
'#description' => $this->t('The machine name of the view mode.'),
'#eca_token_replacement' => TRUE,
];
return $form;
}
/**
* {@inheritdoc}
*/
public function submitConfigurationForm(array &$form, FormStateInterface $form_state): void {
$this->configuration['new_view_mode'] = $form_state->getValue('new_view_mode');
parent::submitConfigurationForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function access($object, AccountInterface $account = NULL, $return_as_object = FALSE) {
if ($this->getEvent() instanceof ContentEntityViewModeAlter) {
$result = AccessResult::allowed();
$viewMode = $this->tokenService->replaceClear($this->configuration['new_view_mode']);
if ($viewMode === '') {
$result = AccessResult::forbidden();
}
}
else {
$result = AccessResult::forbidden();
}
return $return_as_object ? $result : $result->isAllowed();
}
/**
* {@inheritdoc}
*/
public function execute(mixed $entity = NULL): void {
$viewMode = $this->tokenService->replaceClear($this->configuration['new_view_mode']);
/** @var \Drupal\eca_content\Event\ContentEntityViewModeAlter $event */
$event = $this->getEvent();
$event->setViewMode($viewMode);
}
}
......@@ -36,6 +36,7 @@ use Drupal\eca_content\Event\ContentEntityTranslationDelete;
use Drupal\eca_content\Event\ContentEntityTranslationInsert;
use Drupal\eca_content\Event\ContentEntityUpdate;
use Drupal\eca_content\Event\ContentEntityView;
use Drupal\eca_content\Event\ContentEntityViewModeAlter;
use Drupal\eca_content\Event\FieldSelectionBase;
use Drupal\eca_content\Event\OptionsSelection;
use Drupal\eca_content\Event\ReferenceSelection;
......@@ -204,6 +205,12 @@ class ContentEntityEvent extends EventBase implements CleanupInterface {
'event_class' => ContentEntityView::class,
'tags' => Tag::CONTENT | Tag::RUNTIME | Tag::VIEW | Tag::BEFORE,
],
'viewmodealter' => [
'label' => 'Alter entity view mode',
'event_name' => ContentEntityEvents::VIEWMODEALTER,
'event_class' => ContentEntityViewModeAlter::class,
'tags' => Tag::CONTENT | Tag::RUNTIME | Tag::VIEW | Tag::BEFORE,
],
'prepareview' => [
'label' => 'Prepare content entity view',
'event_name' => ContentEntityEvents::PREPAREVIEW,
......@@ -551,6 +558,11 @@ class ContentEntityEvent extends EventBase implements CleanupInterface {
'ids' => $event->getIds(),
];
}
elseif ($event instanceof ContentEntityViewModeAlter) {
$data += [
'view_mode' => $event->getViewMode(),
];
}
$data += parent::buildEventData();
return $data;
......@@ -569,7 +581,7 @@ class ContentEntityEvent extends EventBase implements CleanupInterface {
)]
public function getData(string $key): mixed {
$event = $this->event;
if ($key === 'entity_view_mode' && ($event instanceof ContentEntityPrepareView || $event instanceof ContentEntityView)) {
if ($key === 'entity_view_mode' && ($event instanceof ContentEntityPrepareView || $event instanceof ContentEntityView || $event instanceof ContentEntityViewModeAlter)) {
return $event->getViewMode();
}
return parent::getData($key);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment