class AddContentMenu in Dashboards with Layout Builder 8
Same name and namespace in other branches
- 2.0.x src/Plugin/Dashboard/AddContentMenu.php \Drupal\dashboards\Plugin\Dashboard\AddContentMenu
Show account info.
Plugin annotation
@Dashboard(
id = "add_content_menu",
label = @Translation("Add content"),
category = @Translation("Navigation")
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\dashboards\Plugin\DashboardBase implements ContainerFactoryPluginInterface, DashboardInterface uses StringTranslationTrait
- class \Drupal\dashboards\Plugin\Dashboard\AddContentMenu
- class \Drupal\dashboards\Plugin\DashboardBase implements ContainerFactoryPluginInterface, DashboardInterface uses StringTranslationTrait
Expanded class hierarchy of AddContentMenu
File
- src/
Plugin/ Dashboard/ AddContentMenu.php, line 24
Namespace
Drupal\dashboards\Plugin\DashboardView source
class AddContentMenu extends DashboardBase {
/**
* AccountInterface definition.
*
* @var \Drupal\Core\Session\AccountInterface
*/
protected $account;
/**
* Entity bundle info.
*
* @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
*/
protected $bundleInfo;
/**
* EntityTypeManagerInterface.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* {@inheritdoc}
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, CacheBackendInterface $cache, AccountInterface $account, EntityTypeManagerInterface $entity_type_manager, EntityTypeBundleInfoInterface $bundle_info) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $cache);
$this->account = $account;
$this->entityTypeManager = $entity_type_manager;
$this->bundleInfo = $bundle_info;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('dashboards.cache'), $container
->get('current_user'), $container
->get('entity_type.manager'), $container
->get('entity_type.bundle.info'));
}
/**
* {@inheritdoc}
*/
public function buildRenderArray($configuration) : array {
$bundleInfo = $this->bundleInfo
->getBundleInfo('node');
$items = [];
foreach ($configuration['items'] as $bundle => $info) {
if (AccessResult::allowedIfHasPermission($this->account, 'create ' . $bundle . ' content')
->isAllowed()) {
$items[] = [
'#type' => 'link',
'#url' => Url::fromRoute('node.add', [
'node_type' => $bundle,
]),
'#title' => $bundleInfo[$bundle]['label'],
];
}
}
return [
'menu' => [
'#theme' => 'item_list',
'#items' => $items,
'#attributes' => [
'class' => [
'dashboard-create-content-links',
],
],
],
'#cache' => [
'contexts' => [
'user.permissions',
],
],
];
}
/**
* {@inheritdoc}
*/
public function buildSettingsForm(array $form, FormStateInterface $form_state, array $configuration) : array {
$group_class = 'group-order-weight';
$info = $this->bundleInfo
->getBundleInfo('node');
$form['items'] = [
'#type' => 'table',
'#caption' => $this
->t('Menü items'),
'#header' => [
$this
->t('Label'),
$this
->t('Weight'),
],
'#empty' => $this
->t('No items.'),
'#tableselect' => FALSE,
'#tabledrag' => [
[
'action' => 'order',
'relationship' => 'sibling',
'group' => $group_class,
],
],
];
foreach ($info as $key => $value) {
$form['items'][$key]['#attributes']['class'][] = 'draggable';
$form['items'][$key]['#weight'] = !isset($value['weight']) ? 0 : $value['weight'];
// Label col.
$form['items'][$key]['label'] = [
'#plain_text' => $value['label'],
];
// Weight col.
$form['items'][$key]['weight'] = [
'#type' => 'weight',
'#title' => $this
->t('Weight for @title', [
'@title' => $value['label'],
]),
'#title_display' => 'invisible',
'#default_value' => !isset($value['weight']) ? 0 : $value['weight'],
'#attributes' => [
'class' => [
$group_class,
],
],
];
}
return $form;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
AddContentMenu:: |
protected | property | AccountInterface definition. | |
AddContentMenu:: |
protected | property | Entity bundle info. | |
AddContentMenu:: |
protected | property | EntityTypeManagerInterface. | |
AddContentMenu:: |
public | function |
Build render array. Overrides DashboardBase:: |
|
AddContentMenu:: |
public | function |
Build render array. Overrides DashboardBase:: |
|
AddContentMenu:: |
public static | function |
Creates an instance of the plugin. Overrides DashboardBase:: |
|
AddContentMenu:: |
public | function |
Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides DashboardBase:: |
|
DashboardBase:: |
protected | property | Cache backend. | |
DashboardBase:: |
protected | function | Get cache for cid. | |
DashboardBase:: |
public | function | Validate settings form. | |
DashboardBase:: |
protected | function | Set a new cache entry. Cache is prefixed by pluginid. | |
DashboardBase:: |
public | function | Validate settings form. | 1 |
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. | |
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. |