From ebd5d501a78fb58258a0b0ed511d81942ca8b046 Mon Sep 17 00:00:00 2001 From: "axel.rutz" Date: Tue, 28 May 2019 13:43:51 -0400 Subject: [PATCH] Issue #2909853 by axel.rutz: Allow setting default value on field_domain_access --- .../config/schema/domain_access.schema.yml | 7 +++ domain_access/domain_access.install | 39 ++++++++++++ domain_access/domain_access.module | 59 ++++++++++++++++++- .../src/DomainAccessManagerInterface.php | 2 + 4 files changed, 106 insertions(+), 1 deletion(-) diff --git a/domain_access/config/schema/domain_access.schema.yml b/domain_access/config/schema/domain_access.schema.yml index 63676c66..a09d87a1 100644 --- a/domain_access/config/schema/domain_access.schema.yml +++ b/domain_access/config/schema/domain_access.schema.yml @@ -8,6 +8,13 @@ domain_access.settings: node_advanced_tab_open: type: boolean label: 'Open the Domain Access details by default.' +field.field.*.*.*.third_party.domain_access: + type: mapping + label: 'Domain access default value settings' + mapping: + add_current_domain: + type: boolean + label: 'Add current domain to default values' action.configuration.domain_access_all_action: type: action_configuration_default label: 'Assign content to all affiliates' diff --git a/domain_access/domain_access.install b/domain_access/domain_access.install index 189ea8ff..456bacfa 100644 --- a/domain_access/domain_access.install +++ b/domain_access/domain_access.install @@ -44,3 +44,42 @@ function domain_access_update_8001() { $config->set('node_advanced_tab_open', 0); $config->save(TRUE); } + +/** + * Remove default value callback from domain access fields. + */ +function domain_access_update_8002() { + $field_config_storage = \Drupal::entityTypeManager()->getStorage('field_config'); + $field_configs = $field_config_storage->loadByProperties(['field_name' => DOMAIN_ACCESS_FIELD]); + + if ($field_configs) { + /** @var \Drupal\Core\Field\FieldConfigInterface $field_config */ + foreach ($field_configs as $field_config) { + $field_config->setDefaultValueCallback(NULL); + $field_config->setDefaultValue([]); + $field_config->save(); + $field_ids[] = $field_config->id(); + } + } + return t('Removed default value callbacks from all domain_access fields: @fields', ['@fields' => implode(', ', $field_ids)]); +} + +/** + * Set "add current domain" for required domain_access fields. + */ +function domain_access_update_8003() { + $field_config_storage = \Drupal::entityTypeManager()->getStorage('field_config'); + $field_configs = $field_config_storage->loadByProperties(['field_name' => DOMAIN_ACCESS_FIELD]); + + if ($field_configs) { + /** @var \Drupal\Core\Field\FieldConfigInterface $field_config */ + foreach ($field_configs as $field_config) { + if ($field_config->isRequired()) { + $field_config->setThirdPartySetting('domain_access', 'add_current_domain', TRUE); + $field_config->save(); + $field_ids[] = $field_config->id(); + } + } + } + return t('Set "add current domain" for required domain_access fields: @fields', ['@fields' => implode(', ', $field_ids)]); +} diff --git a/domain_access/domain_access.module b/domain_access/domain_access.module index aeeb1a6f..677edb0d 100755 --- a/domain_access/domain_access.module +++ b/domain_access/domain_access.module @@ -5,8 +5,12 @@ * Domain-based access control for content. */ +use Drupal\Core\Entity\ContentEntityInterface; +use Drupal\Core\Entity\EntityFormInterface; +use Drupal\Core\Field\FieldConfigInterface; use Drupal\Core\Field\FieldDefinitionInterface; use Drupal\Core\Field\FieldItemListInterface; +use Drupal\Core\Form\FormStateInterface; use Drupal\node\NodeInterface; use Drupal\Core\Access\AccessResult; use Drupal\Core\Entity\EntityInterface; @@ -544,7 +548,6 @@ function domain_access_confirm_fields($entity_type, $bundle, array $text = []) { // Users should not be required to be a domain editor. 'required' => $entity_type !== 'user', 'description' => 'Select the affiliate domain(s) for this ' . $text[$entity_type]['name'], - 'default_value_callback' => 'Drupal\domain_access\DomainAccessManager::getDefaultValue', 'settings' => [ 'handler' => 'default:domain', // Handler_settings are deprecated but seem to be necessary here. @@ -744,3 +747,57 @@ function domain_access_default_form_values(&$form, &$form_state, $entity) { $form['field_domain_access']['widget']['#default_value'] = array_keys($values); } } + +/** + * Implements hook_entity_prepare_form(). + * + * If configured, "add_current_domain" to a field_domain_access instance. + */ +function domain_access_entity_prepare_form(EntityInterface $entity, $operation, FormStateInterface $form_state) { + if ($entity instanceof ContentEntityInterface) { + if ($entity->hasField(DOMAIN_ACCESS_FIELD)) { + $field = $entity->get(DOMAIN_ACCESS_FIELD); + /** @var \Drupal\Core\Field\FieldConfigInterface $field_definition */ + $field_definition = $field->getFieldDefinition(); + // Use getValue() as isEmpty() sees empty array as nonempty. + if ($operation === 'default' && $entity->isNew() + && $field_definition->getThirdPartySetting('domain_access', 'add_current_domain')) { + /** @var \Drupal\domain\DomainInterface $current_domain */ + if ($current_domain = \Drupal::service('domain.negotiator')->getActiveDomain()) { + // Merge in current domain, i.e. set it only if it is not set yet. + /** @var \Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem $item */ + foreach ($field as $item) { + if ($item->get('target_id')->getValue() === $current_domain->id()) { + $contains_current_domain = TRUE; + } + } + if (empty($contains_current_domain)) { + $field[] = ['target_id' => $current_domain->id()]; + } + } + } + } + } +} + +/** + * Implements hook_form_FORM_ID_alter() for field_config_edit_form. + * + * Adds our "add_current_domain" setting to field_domain_access instances. + */ +function domain_access_form_field_config_edit_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) { + $form_object = $form_state->getFormObject(); + if ($form_object instanceof EntityFormInterface) { + $config = $form_object->getEntity(); + if ($config instanceof FieldConfigInterface && $config->getName() === DOMAIN_ACCESS_FIELD) { + $subform =& $form['third_party_settings']['domain_access']; + $subform['#type'] = 'fieldset'; + $subform['#title'] = t('Domain access settings'); + $subform['add_current_domain'] = [ + '#title' => t('Add current domain to default values'), + '#type' => 'checkbox', + '#default_value' => $config->getThirdPartySetting('domain_access', 'add_current_domain'), + ]; + } + } +} diff --git a/domain_access/src/DomainAccessManagerInterface.php b/domain_access/src/DomainAccessManagerInterface.php index 7f2f67dd..f944d0f8 100644 --- a/domain_access/src/DomainAccessManagerInterface.php +++ b/domain_access/src/DomainAccessManagerInterface.php @@ -59,6 +59,8 @@ public function checkEntityAccess(FieldableEntityInterface $entity, AccountInter * * @return array * The default field value(s). + * + * @deprecated Will be removed in 8.x-2.x or 9.x-1.x. */ public static function getDefaultValue(FieldableEntityInterface $entity, FieldDefinitionInterface $definition);