From 052e106fc22ddfa2824d53ddf90f04abfbdc6182 Mon Sep 17 00:00:00 2001 From: Jotham Gates Date: Mon, 18 Apr 2022 21:58:53 +1000 Subject: [PATCH] Works with assets and logs --- .../system.action.asset_assign_action.yml | 11 +++ .../owner/config/schema/farm_owner.schema.yml | 4 +- modules/core/owner/farm_owner.routing.yml | 10 ++- ...ignActionForm.php => AssignActionForm.php} | 40 ++++++--- .../owner/src/Plugin/Action/AssetAssign.php | 17 ++++ .../owner/src/Plugin/Action/AssignBase.php | 89 +++++++++++++++++++ .../owner/src/Plugin/Action/LogAssign.php | 87 +----------------- 7 files changed, 159 insertions(+), 99 deletions(-) create mode 100644 modules/core/owner/config/optional/system.action.asset_assign_action.yml rename modules/core/owner/src/Form/{LogAssignActionForm.php => AssignActionForm.php} (83%) create mode 100644 modules/core/owner/src/Plugin/Action/AssetAssign.php create mode 100644 modules/core/owner/src/Plugin/Action/AssignBase.php diff --git a/modules/core/owner/config/optional/system.action.asset_assign_action.yml b/modules/core/owner/config/optional/system.action.asset_assign_action.yml new file mode 100644 index 000000000..31ec32a22 --- /dev/null +++ b/modules/core/owner/config/optional/system.action.asset_assign_action.yml @@ -0,0 +1,11 @@ +langcode: en +status: true +dependencies: + module: + - farm_owner + - asset +id: asset_assign_action +label: 'Assign asset' +type: asset +plugin: 'asset_assign_action' +configuration: { } diff --git a/modules/core/owner/config/schema/farm_owner.schema.yml b/modules/core/owner/config/schema/farm_owner.schema.yml index 9ca184dfd..80c58d494 100644 --- a/modules/core/owner/config/schema/farm_owner.schema.yml +++ b/modules/core/owner/config/schema/farm_owner.schema.yml @@ -1,4 +1,4 @@ # Schema for actions. -action.configuration.log_assign_action: +action.configuration.assign_action: type: action_configuration_default - label: 'Configuration for the log assign action' + label: 'Configuration for the assign action' diff --git a/modules/core/owner/farm_owner.routing.yml b/modules/core/owner/farm_owner.routing.yml index 8111c7e8d..fe98b2559 100644 --- a/modules/core/owner/farm_owner.routing.yml +++ b/modules/core/owner/farm_owner.routing.yml @@ -1,6 +1,14 @@ farm_owner.log_assign_action_form: path: '/log/assign' defaults: - _form: 'Drupal\farm_owner\Form\LogAssignActionForm' + _form: 'Drupal\farm_owner\Form\AssignActionForm' + entity_type: 'log' + requirements: + _user_is_logged_in: 'TRUE' +farm_owner.asset_assign_action_form: + path: '/asset/assign' + defaults: + _form: 'Drupal\farm_owner\Form\AssignActionForm' + entity_type: 'asset' requirements: _user_is_logged_in: 'TRUE' diff --git a/modules/core/owner/src/Form/LogAssignActionForm.php b/modules/core/owner/src/Form/AssignActionForm.php similarity index 83% rename from modules/core/owner/src/Form/LogAssignActionForm.php rename to modules/core/owner/src/Form/AssignActionForm.php index 30e13be93..83f527d36 100644 --- a/modules/core/owner/src/Form/LogAssignActionForm.php +++ b/modules/core/owner/src/Form/AssignActionForm.php @@ -8,14 +8,15 @@ use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Session\AccountInterface; use Drupal\Core\TempStore\PrivateTempStoreFactory; use Drupal\Core\Url; +use Drupal\Component\Plugin\Exception\PluginException; use Drupal\farm_role\ManagedRolePermissionsManagerInterface; use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\HttpFoundation\RedirectResponse; /** - * Provides a log assign confirmation form. + * Provides an assign confirmation form. */ -class LogAssignActionForm extends ConfirmFormBase { +class AssignActionForm extends ConfirmFormBase { /** * The tempstore factory. @@ -53,14 +54,14 @@ class LogAssignActionForm extends ConfirmFormBase { protected $entityType; /** - * The logs to assign. + * The entities to assign. * * @var \Drupal\Core\Entity\EntityInterface[] */ protected $entities; /** - * Constructs a LogAssignActionForm form object. + * Constructs an AssignActionForm form object. * * @param \Drupal\Core\TempStore\PrivateTempStoreFactory $temp_store_factory * The tempstore factory. @@ -72,7 +73,7 @@ class LogAssignActionForm extends ConfirmFormBase { * The current user. */ public function __construct(PrivateTempStoreFactory $temp_store_factory, EntityTypeManagerInterface $entity_type_manager, ManagedRolePermissionsManagerInterface $managed_role_permissions_manager, AccountInterface $user) { - $this->tempStore = $temp_store_factory->get('log_assign_confirm'); + $this->tempStore = $temp_store_factory->get('entity_assign_confirm'); $this->entityTypeManager = $entity_type_manager; $this->managedRolePermissionsManager = $managed_role_permissions_manager; $this->user = $user; @@ -94,7 +95,7 @@ class LogAssignActionForm extends ConfirmFormBase { * {@inheritdoc} */ public function getFormId() { - return 'log_assign_action_confirm_form'; + return 'assign_action_confirm_form'; } /** @@ -136,8 +137,21 @@ class LogAssignActionForm extends ConfirmFormBase { /** * {@inheritdoc} */ - public function buildForm(array $form, FormStateInterface $form_state) { - $this->entityType = $this->entityTypeManager->getDefinition('log'); + public function buildForm(array $form, FormStateInterface $form_state, string $entity_type = NULL) { + $entity_name = ''; + switch($entity_type) { + case 'log': + $this->entityType = $this->entityTypeManager->getDefinition('log'); + $entity_name = 'log'; + break; + case 'asset': + $this->entityType = $this->entityTypeManager->getDefinition('asset'); + $entity_namel = 'asset'; + break; + default: + throw new PluginException('Unsupported entity type given when building form to assign entity'); + } + $this->entities = $this->tempStore->get($this->user->id()); if (empty($this->entityType) || empty($this->entities)) { return new RedirectResponse($this->getCancelUrl() @@ -157,8 +171,8 @@ class LogAssignActionForm extends ConfirmFormBase { $form['users'] = [ '#type' => 'select', - '#title' => $this->t('Assign log(s) to'), - '#description' => $this->t('Select people to assign these logs to.'), + '#title' => $this->t("Assign ${entity_name}(s) to"), + '#description' => $this->t("Select people to assign these ${entity_name}s to."), '#options' => $user_options, '#multiple' => TRUE, ]; @@ -166,7 +180,7 @@ class LogAssignActionForm extends ConfirmFormBase { $form['operation'] = [ '#type' => 'radios', '#title' => $this->t('Append or replace'), - '#description' => $this->t('Select "Append" if you want to add users to the logs, but keep the existing assignments. Select "Replace" if you want to replace existing assignments with the ones specified above.'), + '#description' => $this->t("Select \"Append\" if you want to add users to the ${entity_name}s, but keep the existing assignments. Select \"Replace\" if you want to replace existing assignments with the ones specified above."), '#options' => [ 'append' => $this->t('Append'), 'replace' => $this->t('Replace'), @@ -215,11 +229,11 @@ class LogAssignActionForm extends ConfirmFormBase { $owner_field->appendItem($owner); } - // Validate the log before saving. + // Validate the entity before saving. $violations = $entity->validate(); if ($violations->count() > 0) { $this->messenger()->addWarning( - $this->t('Could not assign log %entity_label: validation failed.', + $this->t('Could not assign %entity_label: validation failed.', [ ':entity_link' => $entity->toUrl()->setAbsolute()->toString(), '%entity_label' => $entity->label(), diff --git a/modules/core/owner/src/Plugin/Action/AssetAssign.php b/modules/core/owner/src/Plugin/Action/AssetAssign.php new file mode 100644 index 000000000..91f961d55 --- /dev/null +++ b/modules/core/owner/src/Plugin/Action/AssetAssign.php @@ -0,0 +1,17 @@ +currentUser = $current_user; + $this->tempStore = $temp_store_factory->get('entity_assign_confirm'); + + parent::__construct($configuration, $plugin_id, $plugin_definition, $entity_type_manager); + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { + return new static( + $configuration, + $plugin_id, + $plugin_definition, + $container->get('entity_type.manager'), + $container->get('tempstore.private'), + $container->get('current_user') + ); + } + + /** + * {@inheritdoc} + */ + public function executeMultiple(array $entities) { + /** @var \Drupal\Core\Entity\EntityInterface[] $entities */ + $this->tempStore->set($this->currentUser->id(), $entities); + } + + /** + * {@inheritdoc} + */ + public function execute($object = NULL) { + $this->executeMultiple([$object]); + } + + /** + * {@inheritdoc} + */ + public function access($object, AccountInterface $account = NULL, $return_as_object = FALSE) { + return $object->access('update', $account, $return_as_object); + } + +} diff --git a/modules/core/owner/src/Plugin/Action/LogAssign.php b/modules/core/owner/src/Plugin/Action/LogAssign.php index 8f4c2641b..9b93d6f1c 100644 --- a/modules/core/owner/src/Plugin/Action/LogAssign.php +++ b/modules/core/owner/src/Plugin/Action/LogAssign.php @@ -2,15 +2,9 @@ namespace Drupal\farm_owner\Plugin\Action; -use Drupal\Core\Action\Plugin\Action\EntityActionBase; -use Drupal\Core\Entity\EntityTypeManagerInterface; -use Drupal\Core\Session\AccountInterface; -use Drupal\Core\TempStore\PrivateTempStoreFactory; -use Symfony\Component\DependencyInjection\ContainerInterface; - /** - * Action that assigns users to logs. - * + * Action that assigns users to logs + * * @Action( * id = "log_assign_action", * label = @Translation("Assign users to logs."), @@ -18,79 +12,6 @@ use Symfony\Component\DependencyInjection\ContainerInterface; * confirm_form_route_name = "farm_owner.log_assign_action_form" * ) */ -class LogAssign extends EntityActionBase { +class LogAssign extends AssignBase { - /** - * The tempstore object. - * - * @var \Drupal\Core\TempStore\SharedTempStore - */ - protected $tempStore; - - /** - * The current user. - * - * @var \Drupal\Core\Session\AccountInterface - */ - protected $currentUser; - - /** - * Constructs a new LogAssign object. - * - * @param array $configuration - * A configuration array containing information about the plugin instance. - * @param string $plugin_id - * The plugin ID for the plugin instance. - * @param mixed $plugin_definition - * The plugin implementation definition. - * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager - * The entity type manager. - * @param \Drupal\Core\TempStore\PrivateTempStoreFactory $temp_store_factory - * The tempstore factory. - * @param \Drupal\Core\Session\AccountInterface $current_user - * Current user. - */ - public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, PrivateTempStoreFactory $temp_store_factory, AccountInterface $current_user) { - $this->currentUser = $current_user; - $this->tempStore = $temp_store_factory->get('log_assign_confirm'); - - parent::__construct($configuration, $plugin_id, $plugin_definition, $entity_type_manager); - } - - /** - * {@inheritdoc} - */ - public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { - return new static( - $configuration, - $plugin_id, - $plugin_definition, - $container->get('entity_type.manager'), - $container->get('tempstore.private'), - $container->get('current_user') - ); - } - - /** - * {@inheritdoc} - */ - public function executeMultiple(array $entities) { - /** @var \Drupal\Core\Entity\EntityInterface[] $entities */ - $this->tempStore->set($this->currentUser->id(), $entities); - } - - /** - * {@inheritdoc} - */ - public function execute($object = NULL) { - $this->executeMultiple([$object]); - } - - /** - * {@inheritdoc} - */ - public function access($object, AccountInterface $account = NULL, $return_as_object = FALSE) { - return $object->access('update', $account, $return_as_object); - } - -} +} \ No newline at end of file