class EntityViewDisplayStatusController
Controller for enabling/disabling entity view displays.
Redirects back to the overview page after toggling status.
Hierarchy
- class \Drupal\Core\Controller\ControllerBase implements \Drupal\Core\DependencyInjection\ContainerInjectionInterface uses \Drupal\Core\DependencyInjection\AutowireTrait, \Drupal\Core\Logger\LoggerChannelTrait, \Drupal\Core\Messenger\MessengerTrait, \Drupal\Core\Routing\RedirectDestinationTrait, \Drupal\Core\StringTranslation\StringTranslationTrait
- class \Drupal\field_ui\Controller\EntityViewDisplayStatusController extends \Drupal\Core\Controller\ControllerBase
Expanded class hierarchy of EntityViewDisplayStatusController
File
-
core/
modules/ field_ui/ src/ Controller/ EntityViewDisplayStatusController.php, line 20
Namespace
Drupal\field_ui\ControllerView source
class EntityViewDisplayStatusController extends ControllerBase {
public function __construct(protected EntityDisplayRepositoryInterface $entityDisplayRepository, EntityTypeManagerInterface $entity_type_manager) {
$this->entityTypeManager = $entity_type_manager;
}
/**
* Enables or disables an entity view display.
*
* @param string $entity_type_id
* The entity type ID.
* @param string $view_mode_name
* The view mode name.
* @param string $operation
* The operation: 'enable' or 'disable'.
* @param \Drupal\Core\Routing\RouteMatchInterface $route_match
* The current route match.
*
* @return \Symfony\Component\HttpFoundation\RedirectResponse
* A redirect to the overview page.
*/
public function toggleStatus(string $entity_type_id, string $view_mode_name, string $operation, RouteMatchInterface $route_match) : RedirectResponse {
// Get bundle from route match.
$entity_type = $this->entityTypeManager
->getDefinition($entity_type_id);
$bundle_entity_type = $entity_type->getBundleEntityType();
if ($bundle_entity_type) {
$bundle_entity = $route_match->getParameter($bundle_entity_type);
$bundle = $bundle_entity ? $bundle_entity->id() : $route_match->getRawParameter($bundle_entity_type);
}
else {
$bundle = $route_match->getRawParameter('bundle') ?: $entity_type_id;
}
$view_modes = $this->entityDisplayRepository
->getViewModes($entity_type_id);
if ($view_mode_name !== 'default' && !isset($view_modes[$view_mode_name])) {
throw new \InvalidArgumentException(sprintf("Invalid view mode '%s' for entity type '%s'.", $view_mode_name, $entity_type_id));
}
// Prevent disabling the default view mode.
if ($view_mode_name === 'default' && $operation === 'disable') {
throw new \InvalidArgumentException('The default view mode cannot be disabled.');
}
$storage = $this->entityTypeManager
->getStorage('entity_view_display');
$default_display_id = $entity_type_id . '.' . $bundle . '.default';
$requested_display_id = $entity_type_id . '.' . $bundle . '.' . $view_mode_name;
$ids_to_load = [
$default_display_id,
];
if ($view_mode_name !== 'default') {
$ids_to_load[] = $requested_display_id;
}
$displays = $storage->loadMultiple($ids_to_load);
if (isset($displays[$requested_display_id])) {
$display = $displays[$requested_display_id];
}
elseif ($view_mode_name !== 'default' && isset($displays[$default_display_id])) {
$display = $displays[$default_display_id]->createCopy($view_mode_name);
}
else {
$display = $storage->create([
'targetEntityType' => $entity_type_id,
'bundle' => $bundle,
'mode' => $view_mode_name,
]);
}
$display->setStatus($operation === 'enable');
$display->save();
$view_mode_label = $view_modes[$view_mode_name]['label'] ?? $view_mode_name;
$message = $operation === 'enable' ? $this->t('The %view_mode view mode has been enabled.', [
'%view_mode' => $view_mode_label,
]) : $this->t('The %view_mode view mode has been disabled.', [
'%view_mode' => $view_mode_label,
]);
$this->messenger()
->addStatus($message);
$route_parameters = FieldUI::getRouteBundleParameter($entity_type, $bundle);
$overview_url = Url::fromRoute("entity.entity_view_display_overview.{$entity_type_id}", $route_parameters);
return new RedirectResponse($overview_url->toString());
}
}
Members
| Title Sort descending | Modifiers | Object type | Summary | Overrides |
|---|---|---|---|---|
| AutowiredInstanceTrait::createInstanceAutowired | public static | function | Instantiates a new instance of the implementing class using autowiring. | |
| AutowiredInstanceTrait::getAutowireArguments | private static | function | Resolves arguments for a method using autowiring. | |
| AutowireTrait::create | public static | function | Instantiates a new instance of the implementing class using autowiring. | 129 |
| ControllerBase::$configFactory | protected | property | The configuration factory. | |
| ControllerBase::$currentUser | protected | property | The current user service. | 2 |
| ControllerBase::$entityFormBuilder | protected | property | The entity form builder. | |
| ControllerBase::$entityTypeManager | protected | property | The entity type manager. | |
| ControllerBase::$formBuilder | protected | property | The form builder. | |
| ControllerBase::$keyValue | protected | property | The key-value storage. | 1 |
| ControllerBase::$languageManager | protected | property | The language manager. | 1 |
| ControllerBase::$moduleHandler | protected | property | The module handler. | 1 |
| ControllerBase::$stateService | protected | property | The state service. | |
| ControllerBase::cache | protected | function | Returns the requested cache bin. | |
| ControllerBase::config | protected | function | Retrieves a configuration object. | |
| ControllerBase::container | private | function | Returns the service container. | |
| ControllerBase::currentUser | protected | function | Returns the current user. | 2 |
| ControllerBase::entityFormBuilder | protected | function | Retrieves the entity form builder. | |
| ControllerBase::entityTypeManager | protected | function | Retrieves the entity type manager. | |
| ControllerBase::formBuilder | protected | function | Returns the form builder service. | |
| ControllerBase::keyValue | protected | function | Returns a key/value storage collection. | 1 |
| ControllerBase::languageManager | protected | function | Returns the language manager service. | 1 |
| ControllerBase::moduleHandler | protected | function | Returns the module handler. | 1 |
| ControllerBase::redirect | protected | function | Returns a redirect response object for the specified route. | |
| ControllerBase::state | protected | function | Returns the state storage service. | |
| EntityViewDisplayStatusController::toggleStatus | public | function | Enables or disables an entity view display. | |
| EntityViewDisplayStatusController::__construct | public | function | ||
| LoggerChannelTrait::$loggerFactory | protected | property | The logger channel factory service. | |
| LoggerChannelTrait::getLogger | protected | function | Gets the logger for a specific channel. | |
| LoggerChannelTrait::setLoggerFactory | public | function | Injects the logger channel factory. | |
| MessengerTrait::$messenger | protected | property | The messenger. | 26 |
| MessengerTrait::messenger | public | function | Gets the messenger. | 26 |
| MessengerTrait::setMessenger | public | function | Sets the messenger. | |
| RedirectDestinationTrait::$redirectDestination | protected | property | The redirect destination service. | 2 |
| RedirectDestinationTrait::getDestinationArray | protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
| RedirectDestinationTrait::getRedirectDestination | protected | function | Returns the redirect destination service. | |
| RedirectDestinationTrait::setRedirectDestination | public | function | Sets the redirect destination service. | |
| StringTranslationTrait::$stringTranslation | protected | property | The string translation service. | 3 |
| StringTranslationTrait::formatPlural | protected | function | Formats a string containing a count of items. | |
| StringTranslationTrait::getNumberOfPlurals | protected | function | Returns the number of plurals supported by a given language. | |
| StringTranslationTrait::getStringTranslation | protected | function | Gets the string translation service. | |
| StringTranslationTrait::setStringTranslation | public | function | Sets the string translation service to use. | 2 |
| StringTranslationTrait::t | protected | function | Translates a string to the current language or to a given language. | 1 |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.