Creates node-level Actions that execute configured media Actions against media referencing the node.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

96 lines
2.5 KiB

<?php
namespace Drupal\node_media_action_bridge\Form;
use Drupal\Core\Action\ActionManager;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Deletes a node media Action wrapper.
*/
class BridgeDeleteForm extends ConfirmFormBase {
/**
* Wrapper ID being deleted.
*/
protected string $wrapperId = '';
/**
* Wrapper configuration.
*/
protected array $wrapper = [];
/**
* Constructs the form.
*/
public function __construct(
protected EntityTypeManagerInterface $entityTypeManager,
protected ActionManager $actionManager,
) {}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container): static {
return new static(
$container->get('entity_type.manager'),
$container->get('plugin.manager.action'),
);
}
/**
* {@inheritdoc}
*/
public function getFormId(): string {
return 'node_media_action_bridge_delete_form';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state, ?string $wrapper_id = NULL): array {
$this->wrapperId = $wrapper_id ?? '';
$wrappers = $this->config('node_media_action_bridge.settings')->get('wrappers') ?: [];
$this->wrapper = $wrappers[$this->wrapperId] ?? [];
return parent::buildForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function getQuestion() {
return $this->t('Delete the node Action %label?', ['%label' => $this->wrapper['label'] ?? $this->wrapperId]);
}
/**
* {@inheritdoc}
*/
public function getCancelUrl(): Url {
return Url::fromRoute('node_media_action_bridge.list');
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state): void {
$config = $this->configFactory()->getEditable('node_media_action_bridge.settings');
$wrappers = $config->get('wrappers') ?: [];
unset($wrappers[$this->wrapperId]);
$config->set('wrappers', $wrappers)->save();
$configured_id = 'node_media_action_bridge_' . $this->wrapperId;
$storage = $this->entityTypeManager->getStorage('action');
if ($action = $storage->load($configured_id)) {
$action->delete();
}
$this->actionManager->clearCachedDefinitions();
$this->messenger()->addStatus($this->t('The node media Action has been deleted.'));
$form_state->setRedirect('node_media_action_bridge.list');
}
}