class EntitySubmenuBlock in Entity Submenu Block 8
Same name in this branch
- 8 src/Plugin/Derivative/EntitySubmenuBlock.php \Drupal\entity_submenu_block\Plugin\Derivative\EntitySubmenuBlock
- 8 src/Plugin/Block/EntitySubmenuBlock.php \Drupal\entity_submenu_block\Plugin\Block\EntitySubmenuBlock
Provides an Entity Submenu Block.
Plugin annotation
@Block(
id = "entity_submenu_block",
admin_label = @Translation("Entity Submenu Block"),
category = @Translation("Menus"),
deriver = "Drupal\entity_submenu_block\Plugin\Derivative\EntitySubmenuBlock"
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Component\Plugin\ContextAwarePluginBase implements ContextAwarePluginInterface
- class \Drupal\Core\Plugin\ContextAwarePluginBase implements CacheableDependencyInterface, ContextAwarePluginInterface uses DependencySerializationTrait, StringTranslationTrait, TypedDataTrait
- class \Drupal\Core\Block\BlockBase implements BlockPluginInterface, PluginWithFormsInterface, PreviewFallbackInterface uses BlockPluginTrait, ContextAwarePluginAssignmentTrait
- class \Drupal\system\Plugin\Block\SystemMenuBlock implements ContainerFactoryPluginInterface
- class \Drupal\entity_submenu_block\Plugin\Block\EntitySubmenuBlock
- class \Drupal\system\Plugin\Block\SystemMenuBlock implements ContainerFactoryPluginInterface
- class \Drupal\Core\Block\BlockBase implements BlockPluginInterface, PluginWithFormsInterface, PreviewFallbackInterface uses BlockPluginTrait, ContextAwarePluginAssignmentTrait
- class \Drupal\Core\Plugin\ContextAwarePluginBase implements CacheableDependencyInterface, ContextAwarePluginInterface uses DependencySerializationTrait, StringTranslationTrait, TypedDataTrait
- class \Drupal\Component\Plugin\ContextAwarePluginBase implements ContextAwarePluginInterface
Expanded class hierarchy of EntitySubmenuBlock
File
- src/
Plugin/ Block/ EntitySubmenuBlock.php, line 24
Namespace
Drupal\entity_submenu_block\Plugin\BlockView source
class EntitySubmenuBlock extends SystemMenuBlock {
/**
* The active menu trail service.
*
* @var \Drupal\Core\Menu\MenuActiveTrailInterface
*/
protected $menuActiveTrail;
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* The entity display repository.
*
* @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface
*/
protected $entityDisplayRepository;
/**
* All entity types.
*
* @var \Drupal\Core\Entity\EntityTypeInterface[]
*/
protected $entityTypes;
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
$static = parent::create($container, $configuration, $plugin_id, $plugin_definition);
$static
->setMenuActiveTrail($container
->get('menu.active_trail'));
$static
->setEntityTypeManager($container
->get('entity_type.manager'));
$static
->setEntityDisplayRepository($container
->get('entity_display.repository'));
$static
->setEntityTypes($container
->get('entity_type.manager')
->getDefinitions());
return $static;
}
public function setMenuActiveTrail(MenuActiveTrailInterface $menuActiveTrail) {
$this->menuActiveTrail = $menuActiveTrail;
}
public function setEntityTypeManager(EntityTypeManagerInterface $entityTypeManager) {
$this->entityTypeManager = $entityTypeManager;
}
public function setEntityDisplayRepository(EntityDisplayRepositoryInterface $entityDisplayRepository) {
$this->entityDisplayRepository = $entityDisplayRepository;
}
public function setEntityTypes(array $entityTypes) {
$this->entityTypes = $entityTypes;
}
/**
* {@inheritdoc}
*/
public function blockForm($form, FormStateInterface $form_state) {
$form = parent::blockForm($form, $form_state);
// We don't need the menu levels options from the parent.
unset($form['menu_levels']);
$config = $this
->getConfiguration();
// Display non-entities.
$form['display_non_entities'] = [
'#title' => $this
->t('Display non-entities'),
'#type' => 'checkbox',
'#default_value' => $this
->getConfigurationValue($config, 'display_non_entities'),
];
// Only display entities with content in current language.
$form['only_current_language'] = [
'#type' => 'checkbox',
'#title' => t('Only display entities with content in current language'),
'#default_value' => $this
->getConfigurationValue($config, 'only_current_language'),
];
// View modes fieldgroup.
$form['view_modes'] = [
'#title' => $this
->t('View modes'),
'#description' => $this
->t('View modes to be used when submenu items are displayed as content entities'),
'#type' => 'fieldgroup',
'#process' => [
[
get_class(),
'processFieldSets',
],
],
];
// A select list of view modes for each entity type.
foreach ($this
->getEntityTypes() as $entity_type) {
$field = 'view_mode_' . $entity_type;
$view_modes = $this->entityDisplayRepository
->getViewModeOptions($entity_type);
$form['view_modes'][$field] = [
'#title' => $this->entityTypeManager
->getDefinition($entity_type)
->getLabel(),
'#type' => 'select',
'#options' => $view_modes,
'#default_value' => $this
->getConfigurationValue($config, $field, array_keys($view_modes)),
];
}
return $form;
}
/**
* Form API callback: Processes the elements in field sets.
*
* Adjusts the #parents of field sets to save its children at the top level.
*/
public static function processFieldSets(&$element, FormStateInterface $form_state, &$complete_form) {
array_pop($element['#parents']);
return $element;
}
/**
* {@inheritdoc}
*/
public function blockSubmit($form, FormStateInterface $form_state) {
foreach ([
'display_non_entities',
'only_current_language',
] as $field) {
$this
->setConfigurationValue($field, $form_state
->getValue($field));
}
foreach ($this
->getEntityTypes() as $entity_type) {
$field = 'view_mode_' . $entity_type;
$value = $form_state
->getValue($field);
$this
->setConfigurationValue($field, $value);
}
}
/**
* {@inheritdoc}
*/
public function build() {
$build = [
'#theme' => 'entity_submenu',
'#menu_name' => NULL,
'#menu_items' => [],
];
// Get the menu name.
$menu_name = $this
->getDerivativeId();
// Return empty menu items array if the active trail is not in this menu.
if (empty($this->menuActiveTrail
->getActiveLink($menu_name))) {
return $build;
}
// The menu name is only set if the active trail is in this menu.
$build['#menu_name'] = $menu_name;
$parameters = $this->menuTree
->getCurrentRouteMenuTreeParameters($menu_name);
// Get current level from end of active trail.
$level = count($parameters->activeTrail);
$parameters
->setMinDepth($level);
// We only want the current level.
$parameters
->setMaxDepth($level);
// We only want enabled links.
$parameters
->onlyEnabledLinks();
$tree = $this->menuTree
->load($menu_name, $parameters);
$manipulators = [
[
'callable' => 'menu.default_tree_manipulators:checkAccess',
],
[
'callable' => 'menu.default_tree_manipulators:generateIndexAndSort',
],
];
$tree = $this->menuTree
->transform($tree, $manipulators);
$config = $this
->getConfiguration();
$language = \Drupal::languageManager()
->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)
->getId();
foreach ($tree as $element) {
// Skip inaccessible links.
if ($element->link instanceof InaccessibleMenuLink) {
continue;
}
$url = $element->link
->getUrlObject();
// Only try to get route parameters from routed links.
if ($url
->isRouted()) {
$routeParams = $url
->getRouteParameters();
reset($routeParams);
$entity_type = key($routeParams);
if (in_array($entity_type, $this
->getEntityTypes())) {
// The link is an entity link.
$entity = $this->entityTypeManager
->getStorage($entity_type)
->load($routeParams[$entity_type]);
if ($this
->getConfigurationValue($config, 'only_current_language') == 1) {
$languages = $entity
->getTranslationLanguages();
if (!array_key_exists($language, $languages)) {
// Skip this entity as content is not translated in current language.
continue;
}
}
// Get render array and continue to next menu item.
$build['#menu_items'][] = $this->entityTypeManager
->getViewBuilder($entity_type)
->view($entity, $config['view_mode_' . $entity_type]);
continue;
}
}
// The link is a routed non-entity link or an external link.
// If the configuration option is set, create a render array.
if ($this
->getConfigurationValue($config, 'display_non_entities') == 1) {
$build['#menu_items'][] = [
'#theme' => 'entity_submenu_item',
'#url' => $url,
'#title' => $element->link
->getTitle(),
];
}
}
return $build;
}
/**
* Returns a configuration value for a specified field.
*
* @param array $config
* Array containing the configuration.
* @param string $field
* Name of the field to get a value for.
* @param array $valid
* Optional array containing valid values for the field.
*
* @return value
* Value for the field or NULL.
*/
protected function getConfigurationValue(array $config, $field, array $valid = NULL) {
$value = NULL;
if (isset($config[$field]) && !empty($config[$field])) {
if (is_array($valid)) {
if (in_array($config[$field], $valid)) {
$value = $config[$field];
}
}
else {
$value = $config[$field];
}
}
return $value;
}
/**
* Returns a list of valid entity types.
*
* @return array
* Valid entity type names.
*/
protected function getEntityTypes() {
$entity_types = [
'node',
];
foreach ($this->entityTypes as $entity_type => $definition) {
if ($entity_type != 'node' && $this
->isValidEntity($entity_type)) {
$entity_types[] = $entity_type;
}
}
return $entity_types;
}
/**
* Filters entities based on their view builder handlers.
*
* @param string $entity_type
* The entity type of the entity that needs to be validated.
*
* @return bool
* TRUE if the entity has the correct view builder handler, FALSE if the
* entity doesn't have the correct view builder handler.
*/
protected function isValidEntity($entity_type) {
return $this->entityTypes[$entity_type]
->get('field_ui_base_route') && $this->entityTypes[$entity_type]
->hasViewBuilderClass();
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
BlockPluginInterface:: |
constant | Indicates the block label (title) should be displayed to end users. | ||
BlockPluginTrait:: |
protected | property | The transliteration service. | |
BlockPluginTrait:: |
public | function | ||
BlockPluginTrait:: |
protected | function | Returns generic default configuration for block plugins. | |
BlockPluginTrait:: |
protected | function | Indicates whether the block should be shown. | 16 |
BlockPluginTrait:: |
public | function | 3 | |
BlockPluginTrait:: |
public | function | Creates a generic configuration form for all block types. Individual block plugins can add elements to this form by overriding BlockBase::blockForm(). Most block plugins should not override this method unless they need to alter the generic form elements. | 2 |
BlockPluginTrait:: |
public | function | ||
BlockPluginTrait:: |
public | function | 1 | |
BlockPluginTrait:: |
public | function | 1 | |
BlockPluginTrait:: |
public | function | 3 | |
BlockPluginTrait:: |
public | function | ||
BlockPluginTrait:: |
public | function | ||
BlockPluginTrait:: |
public | function | ||
BlockPluginTrait:: |
public | function | Sets the transliteration service. | |
BlockPluginTrait:: |
public | function | Most block plugins should not override this method. To add submission handling for a specific block type, override BlockBase::blockSubmit(). | |
BlockPluginTrait:: |
protected | function | Wraps the transliteration service. | |
BlockPluginTrait:: |
public | function | Most block plugins should not override this method. To add validation for a specific block type, override BlockBase::blockValidate(). | 1 |
ContextAwarePluginAssignmentTrait:: |
protected | function | Builds a form element for assigning a context to a given slot. | |
ContextAwarePluginAssignmentTrait:: |
protected | function | Wraps the context handler. | |
ContextAwarePluginBase:: |
protected | property | The data objects representing the context of this plugin. | |
ContextAwarePluginBase:: |
private | property | Data objects representing the contexts passed in the plugin configuration. | |
ContextAwarePluginBase:: |
protected | function |
Overrides ContextAwarePluginBase:: |
|
ContextAwarePluginBase:: |
public | function |
The maximum age for which this object may be cached. Overrides CacheableDependencyInterface:: |
7 |
ContextAwarePluginBase:: |
public | function |
This code is identical to the Component in order to pick up a different
Context class. Overrides ContextAwarePluginBase:: |
|
ContextAwarePluginBase:: |
public | function |
Overrides ContextAwarePluginBase:: |
|
ContextAwarePluginBase:: |
public | function |
Overrides ContextAwarePluginBase:: |
|
ContextAwarePluginBase:: |
public | function |
Gets a mapping of the expected assignment names to their context names. Overrides ContextAwarePluginInterface:: |
|
ContextAwarePluginBase:: |
public | function |
Gets the defined contexts. Overrides ContextAwarePluginInterface:: |
|
ContextAwarePluginBase:: |
public | function |
Gets the value for a defined context. Overrides ContextAwarePluginInterface:: |
|
ContextAwarePluginBase:: |
public | function |
Gets the values for all defined contexts. Overrides ContextAwarePluginInterface:: |
|
ContextAwarePluginBase:: |
public | function |
Set a context on this plugin. Overrides ContextAwarePluginBase:: |
|
ContextAwarePluginBase:: |
public | function |
Sets a mapping of the expected assignment names to their context names. Overrides ContextAwarePluginInterface:: |
|
ContextAwarePluginBase:: |
public | function |
Sets the value for a defined context. Overrides ContextAwarePluginBase:: |
|
ContextAwarePluginBase:: |
public | function |
Validates the set values for the defined contexts. Overrides ContextAwarePluginInterface:: |
|
ContextAwarePluginBase:: |
public | function | Implements magic __get() method. | |
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 | |
EntitySubmenuBlock:: |
protected | property | The entity display repository. | |
EntitySubmenuBlock:: |
protected | property | The entity type manager. | |
EntitySubmenuBlock:: |
protected | property | All entity types. | |
EntitySubmenuBlock:: |
protected | property |
The active menu trail service. Overrides SystemMenuBlock:: |
|
EntitySubmenuBlock:: |
public | function |
Overrides SystemMenuBlock:: |
|
EntitySubmenuBlock:: |
public | function |
Overrides SystemMenuBlock:: |
|
EntitySubmenuBlock:: |
public | function |
Builds and returns the renderable array for this block plugin. Overrides SystemMenuBlock:: |
|
EntitySubmenuBlock:: |
public static | function |
Creates an instance of the plugin. Overrides SystemMenuBlock:: |
|
EntitySubmenuBlock:: |
protected | function | Returns a configuration value for a specified field. | |
EntitySubmenuBlock:: |
protected | function | Returns a list of valid entity types. | |
EntitySubmenuBlock:: |
protected | function | Filters entities based on their view builder handlers. | |
EntitySubmenuBlock:: |
public static | function | Form API callback: Processes the elements in field sets. | |
EntitySubmenuBlock:: |
public | function | ||
EntitySubmenuBlock:: |
public | function | ||
EntitySubmenuBlock:: |
public | function | ||
EntitySubmenuBlock:: |
public | function | ||
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. | |
PluginWithFormsTrait:: |
public | function | ||
PluginWithFormsTrait:: |
public | function | ||
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. | |
SystemMenuBlock:: |
protected | property | The menu link tree service. | |
SystemMenuBlock:: |
public | function |
Overrides BlockPluginTrait:: |
|
SystemMenuBlock:: |
public | function |
The cache contexts associated with this object. Overrides ContextAwarePluginBase:: |
|
SystemMenuBlock:: |
public | function |
The cache tags associated with this object. Overrides ContextAwarePluginBase:: |
|
SystemMenuBlock:: |
public static | function | Form API callback: Processes the menu_levels field element. | |
SystemMenuBlock:: |
public | function |
Constructs a new SystemMenuBlock. Overrides BlockPluginTrait:: |
|
TypedDataTrait:: |
protected | property | The typed data manager used for creating the data types. | |
TypedDataTrait:: |
public | function | Gets the typed data manager. | 2 |
TypedDataTrait:: |
public | function | Sets the typed data manager. | 2 |