diff --git a/core/core.services.yml b/core/core.services.yml index f8c428f..332cb8f 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -281,7 +281,7 @@ services: arguments: ['@config.factory', '@module_handler', '@state', '@info_parser', '@logger.channel.default', '@asset.css.collection_optimizer', '@config.installer', '@router.builder'] entity.manager: class: Drupal\Core\Entity\EntityManager - arguments: ['@container.namespaces', '@module_handler', '@cache.discovery', '@language_manager', '@string_translation', '@class_resolver', '@typed_data_manager'] + arguments: ['@container.namespaces', '@module_handler', '@cache.discovery', '@language_manager', '@string_translation', '@class_resolver', '@typed_data_manager', '@config.factory'] parent: container.trait tags: - { name: plugin_manager_cache_clear } diff --git a/core/lib/Drupal/Core/Entity/EntityManager.php b/core/lib/Drupal/Core/Entity/EntityManager.php index dbee644..6a55145 100644 --- a/core/lib/Drupal/Core/Entity/EntityManager.php +++ b/core/lib/Drupal/Core/Entity/EntityManager.php @@ -10,6 +10,7 @@ use Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException; use Drupal\Component\Plugin\Exception\PluginNotFoundException; use Drupal\Component\Utility\String; +use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\DependencyInjection\ClassResolverInterface; use Drupal\Core\Field\BaseFieldDefinition; use Drupal\Core\Cache\Cache; @@ -113,6 +114,13 @@ class EntityManager extends DefaultPluginManager implements EntityManagerInterfa protected $languageManager; /** + * The config factory. + * + * @var \Drupal\Core\Config\ConfigFactoryInterface + */ + protected $configFactory; + + /** * Static cache of bundle information. * * @var array @@ -169,8 +177,12 @@ class EntityManager extends DefaultPluginManager implements EntityManagerInterfa * The string translationManager. * @param \Drupal\Core\DependencyInjection\ClassResolverInterface $class_resolver * The class resolver. + * @param \Drupal\Core\TypedData\TypedDataManager $typed_data_manager + * The typed data manager. + * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory + * The config factory. */ - public function __construct(\Traversable $namespaces, ModuleHandlerInterface $module_handler, CacheBackendInterface $cache, LanguageManagerInterface $language_manager, TranslationInterface $translation_manager, ClassResolverInterface $class_resolver, TypedDataManager $typed_data_manager) { + public function __construct(\Traversable $namespaces, ModuleHandlerInterface $module_handler, CacheBackendInterface $cache, LanguageManagerInterface $language_manager, TranslationInterface $translation_manager, ClassResolverInterface $class_resolver, TypedDataManager $typed_data_manager, ConfigFactoryInterface $config_factory) { parent::__construct('Entity', $namespaces, $module_handler, 'Drupal\Core\Entity\EntityInterface', 'Drupal\Core\Entity\Annotation\EntityType'); $this->setCacheBackend($cache, 'entity_type', array('entity_types' => TRUE)); @@ -180,6 +192,7 @@ public function __construct(\Traversable $namespaces, ModuleHandlerInterface $mo $this->translationManager = $translation_manager; $this->classResolver = $class_resolver; $this->typedDataManager = $typed_data_manager; + $this->configFactory = $config_factory; } /** @@ -890,6 +903,20 @@ public function getFormModeOptions($entity_type_id) { } /** + * {@inheritdoc} + */ + public function getViewModeOptionsByBundle($entity_type_id, $bundle, $include_disabled = FALSE) { + return $this->getDisplayModeOptionsByBundle('view_mode', $entity_type_id, $bundle, $include_disabled); + } + + /** + * {@inheritdoc} + */ + public function getFormModeOptionsByBundle($entity_type_id, $bundle, $include_disabled = FALSE) { + return $this->getDisplayModeOptionsByBundle('form_mode', $entity_type_id, $bundle, $include_disabled); + } + + /** * Returns an array of display mode options. * * @param string $display_type @@ -909,6 +936,49 @@ protected function getDisplayModeOptions($display_type, $entity_type_id) { } /** + * Returns an array of display mode options for a certain bundle. + * + * @param $display_type + * The display type to be retrieved. It can be "view_mode" or "form_mode". + * @param string $entity_type_id + * The entity type whose display mode options should be returned. + * @param string $bundle + * The name of the bundle. + * @param bool $include_disabled + * Force to include disabled view modes. Defaults to FALSE. + * + * @return array + * An array of display mode labels, keyed by the display mode ID. + */ + protected function getDisplayModeOptionsByBundle($display_type, $entity_type_id, $bundle, $include_disabled) { + $load_ids = array(); + $display_mode_options = array(); + $display_type_entity_name = ($display_type == 'form_mode') ? 'entity_form_display' : 'entity_view_display'; + $entity_type = $this->getDefinition($display_type_entity_name); + $config_prefix = $entity_type->getConfigPrefix(); + + // Load all the entity's display modes. + $view_modes = $this->getDisplayModesByEntityType($display_type, $entity_type_id); + + // Get the list of available display modes for the current block's bundle. + $ids = $this->configFactory->listAll($config_prefix . '.' . $entity_type_id . '.' . $bundle); + foreach ($ids as $id) { + $load_ids[] = str_replace($config_prefix . '.', '', $id); + } + $displays = entity_load_multiple('entity_view_display', $load_ids); + + // Generate the display options array. + foreach ($displays as $display) { + if ($display->status() || $include_disabled) { + $display_mode_name = $display->get('mode'); + $display_mode_options[$display_mode_name] = ($display_mode_name == 'default') ? t('Default') : $view_modes[$display_mode_name]['label']; + } + } + + return $display_mode_options; + } + + /** * {@inheritdoc} */ public function loadEntityByUuid($entity_type_id, $uuid) { diff --git a/core/lib/Drupal/Core/Entity/EntityManagerInterface.php b/core/lib/Drupal/Core/Entity/EntityManagerInterface.php index 2d877d6..d78df41 100644 --- a/core/lib/Drupal/Core/Entity/EntityManagerInterface.php +++ b/core/lib/Drupal/Core/Entity/EntityManagerInterface.php @@ -353,6 +353,36 @@ public function getViewModeOptions($entity_type_id); public function getFormModeOptions($entity_type_id); /** + * Returns an array of view mode options by bundle. + * + * @param string $entity_type_id + * The entity type whose view mode options should be returned. + * @param string $bundle + * The name of the bundle. + * @param bool $include_disabled + * Force to include disabled view modes. Defaults to FALSE. + * + * @return array + * An array of view mode labels, keyed by the display mode ID. + */ + public function getViewModeOptionsByBundle($entity_type_id, $bundle, $include_disabled = FALSE); + + /** + * Returns an array of form mode options by bundle. + * + * @param string $entity_type_id + * The entity type whose form mode options should be returned. + * @param string $bundle + * The name of the bundle. + * @param bool $include_disabled + * Force to include disabled form modes. Defaults to FALSE. + * + * @return array + * An array of form mode labels, keyed by the display mode ID. + */ + public function getFormModeOptionsByBundle($entity_type_id, $bundle, $include_disabled = FALSE); + + /** * Loads an entity by UUID. * * Note that some entity types may not support UUIDs. diff --git a/core/modules/block_content/src/Plugin/Block/BlockContentBlock.php b/core/modules/block_content/src/Plugin/Block/BlockContentBlock.php index 1219ff9..054ef70 100644 --- a/core/modules/block_content/src/Plugin/Block/BlockContentBlock.php +++ b/core/modules/block_content/src/Plugin/Block/BlockContentBlock.php @@ -9,9 +9,6 @@ use Drupal\Core\Block\BlockBase; use Drupal\Core\Block\BlockManagerInterface; -use Drupal\Core\Config\ConfigFactoryInterface; -use Drupal\Core\Entity\EntityInterface; -use Drupal\Core\Entity\EntityManager; use Drupal\Core\Entity\EntityManagerInterface; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; @@ -60,13 +57,6 @@ class BlockContentBlock extends BlockBase implements ContainerFactoryPluginInter protected $account; /** - * The config factory. - * - * @var \Drupal\Core\Config\ConfigFactoryInterface - */ - protected $configFactory; - - /** * Constructs a new BlockContentBlock. * * @param array $configuration @@ -83,17 +73,14 @@ class BlockContentBlock extends BlockBase implements ContainerFactoryPluginInter * The Module Handler. * @param \Drupal\Core\Session\AccountInterface $account * The account for which view access should be checked. - * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory - * The configuration factory. */ - public function __construct(array $configuration, $plugin_id, $plugin_definition, BlockManagerInterface $block_manager, EntityManagerInterface $entity_manager, ModuleHandlerInterface $module_handler, AccountInterface $account, ConfigFactoryInterface $config_factory) { + public function __construct(array $configuration, $plugin_id, $plugin_definition, BlockManagerInterface $block_manager, EntityManagerInterface $entity_manager, ModuleHandlerInterface $module_handler, AccountInterface $account) { parent::__construct($configuration, $plugin_id, $plugin_definition); $this->blockManager = $block_manager; $this->entityManager = $entity_manager; $this->moduleHandler = $module_handler; $this->account = $account; - $this->configFactory = $config_factory; } /** @@ -107,8 +94,7 @@ public static function create(ContainerInterface $container, array $configuratio $container->get('plugin.manager.block'), $container->get('entity.manager'), $container->get('module_handler'), - $container->get('current_user'), - $container->get('config.factory') + $container->get('current_user') ); } @@ -139,7 +125,7 @@ public function blockForm($form, FormStateInterface $form_state) { $block = $this->entityManager->loadEntityByUuid('block_content', $uuid); $form['block_content']['view_mode'] = array( '#type' => 'select', - '#options' => $this->getViewModeOptions($block), + '#options' => $this->entityManager->getViewModeOptionsByBundle('block_content', $block->bundle()), '#title' => t('View mode'), '#description' => t('Output the block in this view mode.'), '#default_value' => $this->configuration['view_mode'] @@ -178,39 +164,4 @@ public function build() { } } - /** - * Retrieves the list of available view modes for the current block. - * - * @param EntityInterface $block - * The block to get options. - * - * @return array - * List of available view modes for the current block. - */ - protected function getViewModeOptions(EntityInterface $block) { - $load_ids = array(); - $view_mode_options = array(); - - // Load all the block_content's view modes. - $view_modes = $this->entityManager->getViewModes('block_content'); - - // Get the list of available view modes for the current block's bundle. - $ids = $this->configFactory->listAll('core.entity_view_display.block_content.' . $block->bundle()); - foreach ($ids as $id) { - $config_id = str_replace('core.entity_view_display' . '.', '', $id); - $load_ids[] = $config_id; - } - $displays = entity_load_multiple('entity_view_display', $load_ids); - - // Generate the display options array. - foreach ($displays as $display) { - if ($display->status()) { - $view_mode_name = $display->get('mode'); - $view_mode_options[$view_mode_name] = ($view_mode_name == 'default') ? t('Default') : $view_modes[$view_mode_name]['label']; - } - } - - return $view_mode_options; - } - } diff --git a/core/modules/block_content/src/Tests/BlockContentCreationTest.php b/core/modules/block_content/src/Tests/BlockContentCreationTest.php index d1c3719..5123eb5 100644 --- a/core/modules/block_content/src/Tests/BlockContentCreationTest.php +++ b/core/modules/block_content/src/Tests/BlockContentCreationTest.php @@ -61,7 +61,6 @@ public function testBlockContentCreation() { $this->drupalLogin($this->adminUser); - // Create a block. $edit = array(); $edit['info[0][value]'] = 'Test Block'; diff --git a/core/modules/node/src/Form/NodePreviewForm.php b/core/modules/node/src/Form/NodePreviewForm.php index 81fd61c..ec48909 100644 --- a/core/modules/node/src/Form/NodePreviewForm.php +++ b/core/modules/node/src/Form/NodePreviewForm.php @@ -85,7 +85,7 @@ public function buildForm(array $form, FormStateInterface $form_state, EntityInt '#options' => array('attributes' => array('class' => array('node-preview-backlink'))) + $query_options, ); - $view_mode_options = $this->getViewModeOptions($node); + $view_mode_options = $this->entityManager->getViewModeOptionsByBundle('node', $node->bundle()); $form['uuid'] = array( '#type' => 'value', @@ -123,46 +123,4 @@ public function submitForm(array &$form, FormStateInterface $form_state) { )); } - /** - * Retrieves the list of available view modes for the current node. - * - * @param EntityInterface $node - * The node being previewed. - * - * @return array - * List of available view modes for the current node. - */ - protected function getViewModeOptions(EntityInterface $node) { - $load_ids = array(); - $view_mode_options = array(); - - // Load all the node's view modes. - $view_modes = $this->entityManager->getViewModes('node'); - - // Get the list of available view modes for the current node's bundle. - $ids = $this->configFactory->listAll('core.entity_view_display.node.' . $node->bundle()); - foreach ($ids as $id) { - $config_id = str_replace('core.entity_view_display' . '.', '', $id); - $load_ids[] = $config_id; - } - $displays = entity_load_multiple('entity_view_display', $load_ids); - - // Generate the display options array. - foreach ($displays as $display) { - - $view_mode_name = $display->get('mode'); - - // Skip view modes that are not used in the front end. - if (in_array($view_mode_name, array('rss', 'search_index'))) { - continue; - } - - if ($display->status()) { - $view_mode_options[$view_mode_name] = ($view_mode_name == 'default') ? t('Default') : $view_modes[$view_mode_name]['label']; - } - } - - return $view_mode_options; - } - }