devel_wizard-2.x-dev/src/Plugin/DevelWizard/Spell/NodeTypeAdminViewSpell.php
src/Plugin/DevelWizard/Spell/NodeTypeAdminViewSpell.php
<?php
declare(strict_types=1);
namespace Drupal\devel_wizard\Plugin\DevelWizard\Spell;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\devel_wizard\Attribute\DevelWizardSpell;
/**
* @todo Validate that a views.view with the same name does not exists.
*/
#[DevelWizardSpell(
id: 'devel_wizard_node_type_admin_view',
category: new TranslatableMarkup('Content type'),
label: new TranslatableMarkup('Content type - admin view'),
description: new TranslatableMarkup('Creates a view for administrators for an existing Content type.'),
tags: [
'bundle' => new TranslatableMarkup('Bundle'),
'config_entity' => new TranslatableMarkup('Config entity'),
'node_type' => new TranslatableMarkup('Content type'),
'admin' => new TranslatableMarkup('Admin'),
],
)]
class NodeTypeAdminViewSpell extends ConfigEntityAdminViewSpellBase {
/**
* {@inheritdoc}
*/
protected string $provider = 'node';
protected string $configEntityTypeId = 'node_type';
protected string $contentEntityTypeId = 'node';
protected string $primaryTabPath = 'admin/content/node';
protected string $bundleIdInConfigTemplate = 'article';
/**
* @throws \Drupal\Core\Entity\EntityStorageException
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Core\Entity\EntityMalformedException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
*/
protected function doItTabsCreate(): static {
$viewStorage = $this->entityTypeManager->getStorage('view');
// @todo Config views.view.content" is required.
// @todo Check that if the links.task entry for the main route is exists or not.
$viewRaw = $this->getSecondaryTaskView();
/** @var \Drupal\Core\Config\Entity\ConfigEntityInterface $view */
$view = $viewStorage->load($viewRaw['id']);
if ($view) {
$this->messageConfigEntityExists($view);
return $this;
}
/** @var \Drupal\Core\Config\Entity\ConfigEntityInterface $view */
$view = $viewStorage->create($viewRaw);
$view->save();
$this->messageConfigEntityCreate($view);
return $this;
}
protected function getSecondaryTaskView(): array {
$conf =& $this->configuration;
$data = parent::getSecondaryTaskView();
$data['display']['default']['display_options']['access']['options']['perm'] = "delete any {$conf['machine_name']} content";
$data['display']['overview']['display_options']['path'] = "admin/content/node/list-{$conf['machine_name']}";
return $data;
}
}
