DomainAccessActionBase.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. namespace Drupal\domain_access\Plugin\Action;
  3. use Drupal\Core\Action\ConfigurableActionBase;
  4. use Drupal\Core\Entity\DependencyTrait;
  5. use Drupal\Core\Entity\EntityTypeInterface;
  6. use Drupal\Core\Form\FormStateInterface;
  7. use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
  8. use Drupal\Core\Session\AccountInterface;
  9. use Symfony\Component\DependencyInjection\ContainerInterface;
  10. /**
  11. * Provides a base class for operations to change domain assignments.
  12. */
  13. abstract class DomainAccessActionBase extends ConfigurableActionBase implements ContainerFactoryPluginInterface {
  14. use DependencyTrait;
  15. /**
  16. * The user role entity type.
  17. *
  18. * @var \Drupal\Core\Entity\EntityTypeInterface
  19. */
  20. protected $entityType;
  21. /**
  22. * {@inheritdoc}
  23. */
  24. public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeInterface $entity_type) {
  25. parent::__construct($configuration, $plugin_id, $plugin_definition);
  26. $this->entityType = $entity_type;
  27. }
  28. /**
  29. * {@inheritdoc}
  30. */
  31. public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
  32. return new static(
  33. $configuration,
  34. $plugin_id,
  35. $plugin_definition,
  36. $container->get('entity.manager')->getDefinition('domain')
  37. );
  38. }
  39. /**
  40. * {@inheritdoc}
  41. */
  42. public function defaultConfiguration() {
  43. return [
  44. 'domain_id' => '',
  45. ];
  46. }
  47. /**
  48. * {@inheritdoc}
  49. */
  50. public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
  51. $domains = \Drupal::entityTypeManager()->getStorage('domain')->loadOptionsList();
  52. $form['domain_id'] = [
  53. '#type' => 'checkboxes',
  54. '#title' => t('Domain'),
  55. '#options' => $domains,
  56. '#default_value' => $this->configuration['id'],
  57. '#required' => TRUE,
  58. ];
  59. return $form;
  60. }
  61. /**
  62. * {@inheritdoc}
  63. */
  64. public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
  65. $this->configuration['domain_id'] = $form_state->getValue('domain_id');
  66. }
  67. /**
  68. * {@inheritdoc}
  69. */
  70. public function calculateDependencies() {
  71. if (!empty($this->configuration['domain_id'])) {
  72. $prefix = $this->entityType->getConfigPrefix() . '.';
  73. $this->addDependency('config', $prefix . $this->configuration['domain_id']);
  74. }
  75. return $this->dependencies;
  76. }
  77. /**
  78. * {@inheritdoc}
  79. */
  80. public function access($object, AccountInterface $account = NULL, $return_as_object = FALSE) {
  81. /** @var \Drupal\user\UserInterface $object */
  82. // @TODO: fix this logic.
  83. $access = $object->access('update', $account, TRUE);
  84. return $return_as_object ? $access : $access->isAllowed();
  85. }
  86. }