Skip to content
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
7 changes: 7 additions & 0 deletions domain_access/config/schema/domain_access.schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
39 changes: 39 additions & 0 deletions domain_access/domain_access.install
Original file line number Diff line number Diff line change
Expand Up @@ -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)]);
}
59 changes: 58 additions & 1 deletion domain_access/domain_access.module
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -550,7 +554,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.
Expand Down Expand Up @@ -750,3 +753,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'),
];
}
}
}
2 changes: 2 additions & 0 deletions domain_access/src/DomainAccessManagerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down