Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better handling of actions. #475

Open
wants to merge 1 commit into
base: 8.x-1.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions domain_access/domain_access.install
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,7 @@ function domain_access_update_8001() {
$config->set('node_advanced_tab_open', 0);
$config->save(TRUE);
}

/**
* @TODO: Update action names to replace . with __.
*/
16 changes: 8 additions & 8 deletions domain_access/domain_access.module
Original file line number Diff line number Diff line change
Expand Up @@ -632,7 +632,7 @@ function domain_access_domain_insert($entity) {
// Do not fire hook when config sync in progress.
return;
}
$id = 'domain_access_add_action.' . $entity->id();
$id = 'domain_access_add_action__' . $entity->id();
$controller = \Drupal::entityTypeManager()->getStorage('action');
if (!$controller->load($id)) {
/** @var \Drupal\system\Entity\Action $action */
Expand All @@ -641,49 +641,49 @@ function domain_access_domain_insert($entity) {
'type' => 'node',
'label' => t('Add selected content to the @label domain', ['@label' => $entity->label()]),
'configuration' => [
'domain_id' => $entity->id(),
'domain_id' => [$entity->id()],
],
'plugin' => 'domain_access_add_action',
]);
$action->trustData()->save();
}
$remove_id = 'domain_access_remove_action.' . $entity->id();
$remove_id = 'domain_access_remove_action__' . $entity->id();
if (!$controller->load($remove_id)) {
/** @var \Drupal\system\Entity\Action $action */
$action = $controller->create([
'id' => $remove_id,
'type' => 'node',
'label' => t('Remove selected content from the @label domain', ['@label' => $entity->label()]),
'configuration' => [
'domain_id' => $entity->id(),
'domain_id' => [$entity->id()],
],
'plugin' => 'domain_access_remove_action',
]);
$action->trustData()->save();
}
$id = 'domain_access_add_editor_action.' . $entity->id();
$id = 'domain_access_add_editor_action__' . $entity->id();
if (!$controller->load($id)) {
/** @var \Drupal\system\Entity\Action $action */
$action = $controller->create([
'id' => $id,
'type' => 'user',
'label' => t('Add editors to the @label domain', ['@label' => $entity->label()]),
'configuration' => [
'domain_id' => $entity->id(),
'domain_id' => [$entity->id()],
],
'plugin' => 'domain_access_add_editor_action',
]);
$action->trustData()->save();
}
$remove_id = 'domain_access_remove_editor_action.' . $entity->id();
$remove_id = 'domain_access_remove_editor_action__' . $entity->id();
if (!$controller->load($remove_id)) {
/** @var \Drupal\system\Entity\Action $action */
$action = $controller->create([
'id' => $remove_id,
'type' => 'user',
'label' => t('Remove editors from the @label domain', ['@label' => $entity->label()]),
'configuration' => [
'domain_id' => $entity->id(),
'domain_id' => [$entity->id()],
],
'plugin' => 'domain_access_remove_editor_action',
]);
Expand Down
11 changes: 9 additions & 2 deletions domain_access/src/Plugin/Action/DomainAccessActionBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta
'#type' => 'checkboxes',
'#title' => t('Domain'),
'#options' => $domains,
'#default_value' => $this->configuration['id'],
'#default_value' => isset($this->configuration['domain_id']) ? (array) $this->configuration['domain_id'] : [],
'#required' => TRUE,
];
return $form;
Expand All @@ -81,7 +81,14 @@ public function submitConfigurationForm(array &$form, FormStateInterface $form_s
public function calculateDependencies() {
if (!empty($this->configuration['domain_id'])) {
$prefix = $this->entityType->getConfigPrefix() . '.';
$this->addDependency('config', $prefix . $this->configuration['domain_id']);
if (is_array($this->configuration['domain_id'])) {
foreach ($this->configuration['domain_id'] as $domain_id) {
$this->addDependency('config', $prefix . $domain_id);
}
}
else {
$this->addDependency('config', $prefix . $this->configuration['domain_id']);
}
}
return $this->dependencies;
}
Expand Down
12 changes: 7 additions & 5 deletions domain_access/src/Plugin/Action/DomainAccessAdd.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,16 @@ class DomainAccessAdd extends DomainAccessActionBase {
* {@inheritdoc}
*/
public function execute($entity = NULL) {
$id = $this->configuration['domain_id'];
$ids = (array) $this->configuration['domain_id'];
$node_domains = \Drupal::service('domain_access.manager')->getAccessValues($entity);

// Add domain assignment if not present.
if ($entity !== FALSE && !isset($node_domains[$id])) {
$node_domains[$id] = $id;
$entity->set(DOMAIN_ACCESS_FIELD, array_keys($node_domains));
$entity->save();
foreach ($ids as $id) {
if ($entity !== FALSE && !isset($node_domains[$id])) {
$node_domains[$id] = $id;
$entity->set(DOMAIN_ACCESS_FIELD, array_keys($node_domains));
$entity->save();
}
}
}

Expand Down
12 changes: 7 additions & 5 deletions domain_access/src/Plugin/Action/DomainAccessAddEditor.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,16 @@ class DomainAccessAddEditor extends DomainAccessActionBase {
* {@inheritdoc}
*/
public function execute($entity = NULL) {
$id = $this->configuration['domain_id'];
$ids = (array) $this->configuration['domain_id'];
$user_domains = \Drupal::service('domain_access.manager')->getAccessValues($entity);

// Skip adding the role to the user if they already have it.
if ($entity !== FALSE && !isset($user_domains[$id])) {
$user_domains[$id] = $id;
$entity->set(DOMAIN_ACCESS_FIELD, array_keys($user_domains));
$entity->save();
foreach ($ids as $id) {
if ($entity !== FALSE && !isset($user_domains[$id])) {
$user_domains[$id] = $id;
$entity->set(DOMAIN_ACCESS_FIELD, array_keys($user_domains));
$entity->save();
}
}
}

Expand Down
12 changes: 7 additions & 5 deletions domain_access/src/Plugin/Action/DomainAccessRemove.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,16 @@ class DomainAccessRemove extends DomainAccessActionBase {
* {@inheritdoc}
*/
public function execute($entity = NULL) {
$id = $this->configuration['domain_id'];
$ids = (array) $this->configuration['domain_id'];
$node_domains = \Drupal::service('domain_access.manager')->getAccessValues($entity);

// Remove domain assignment if present.
if ($entity !== FALSE && isset($node_domains[$id])) {
unset($node_domains[$id]);
$entity->set(DOMAIN_ACCESS_FIELD, array_keys($node_domains));
$entity->save();
foreach ($ids as $id) {
if ($entity !== FALSE && isset($node_domains[$id])) {
unset($node_domains[$id]);
$entity->set(DOMAIN_ACCESS_FIELD, array_keys($node_domains));
$entity->save();
}
}
}

Expand Down
12 changes: 7 additions & 5 deletions domain_access/src/Plugin/Action/DomainAccessRemoveEditor.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,16 @@ class DomainAccessRemoveEditor extends DomainAccessActionBase {
* {@inheritdoc}
*/
public function execute($entity = NULL) {
$id = $this->configuration['domain_id'];
$id = (array) $this->configuration['domain_id'];
$user_domains = \Drupal::service('domain_access.manager')->getAccessValues($entity);

// Skip adding the role to the user if they already have it.
if ($entity !== FALSE && isset($user_domains[$id])) {
unset($user_domains[$id]);
$entity->set(DOMAIN_ACCESS_FIELD, array_keys($user_domains));
$entity->save();
foreach ($ids as $id) {
if ($entity !== FALSE && isset($user_domains[$id])) {
unset($user_domains[$id]);
$entity->set(DOMAIN_ACCESS_FIELD, array_keys($user_domains));
$entity->save();
}
}
}

Expand Down