You are here

class AddContentMenu in Dashboards with Layout Builder 8

Same name and namespace in other branches
  1. 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

Expanded class hierarchy of AddContentMenu

File

src/Plugin/Dashboard/AddContentMenu.php, line 24

Namespace

Drupal\dashboards\Plugin\Dashboard
View 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

Namesort descending Modifiers Type Description Overrides
AddContentMenu::$account protected property AccountInterface definition.
AddContentMenu::$bundleInfo protected property Entity bundle info.
AddContentMenu::$entityTypeManager protected property EntityTypeManagerInterface.
AddContentMenu::buildRenderArray public function Build render array. Overrides DashboardBase::buildRenderArray
AddContentMenu::buildSettingsForm public function Build render array. Overrides DashboardBase::buildSettingsForm
AddContentMenu::create public static function Creates an instance of the plugin. Overrides DashboardBase::create
AddContentMenu::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides DashboardBase::__construct
DashboardBase::$cache protected property Cache backend.
DashboardBase::getCache protected function Get cache for cid.
DashboardBase::massageFormValues public function Validate settings form.
DashboardBase::setCache protected function Set a new cache entry. Cache is prefixed by pluginid.
DashboardBase::validateForm public function Validate settings form. 1
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
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.