Works with assets and logs
This commit is contained in:
parent
218ad1c476
commit
052e106fc2
|
@ -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: { }
|
|
@ -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'
|
||||
|
|
|
@ -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'
|
||||
|
|
|
@ -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 <a href=":entity_link">%entity_label</a>: validation failed.',
|
||||
$this->t('Could not assign <a href=":entity_link">%entity_label</a>: validation failed.',
|
||||
[
|
||||
':entity_link' => $entity->toUrl()->setAbsolute()->toString(),
|
||||
'%entity_label' => $entity->label(),
|
|
@ -0,0 +1,17 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\farm_owner\Plugin\Action;
|
||||
|
||||
/**
|
||||
* Action that assigns users to assets
|
||||
*
|
||||
* @Action(
|
||||
* id = "asset_assign_action",
|
||||
* label = @Translation("Assign users to assets."),
|
||||
* type = "asset",
|
||||
* confirm_form_route_name = "farm_owner.asset_assign_action_form"
|
||||
* )
|
||||
*/
|
||||
class AssetAssign extends AssignBase {
|
||||
|
||||
}
|
|
@ -0,0 +1,89 @@
|
|||
<?php
|
||||
|
||||
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 entities.
|
||||
*/
|
||||
class AssignBase extends EntityActionBase {
|
||||
|
||||
/**
|
||||
* The tempstore object.
|
||||
*
|
||||
* @var \Drupal\Core\TempStore\SharedTempStore
|
||||
*/
|
||||
protected $tempStore;
|
||||
|
||||
/**
|
||||
* The current user.
|
||||
*
|
||||
* @var \Drupal\Core\Session\AccountInterface
|
||||
*/
|
||||
protected $currentUser;
|
||||
|
||||
/**
|
||||
* Constructs a new AssignBase 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('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);
|
||||
}
|
||||
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue