Skip to content
Snippets Groups Projects
Commit 5497e41e authored by Joshua Bolduc's avatar Joshua Bolduc
Browse files

Issue #3405229 by partdigital, Odai Atieh: Order of module installation...

Issue #3405229 by partdigital, Odai Atieh: Order of module installation prevents proper access_policy field creation.
parent 78043153
No related branches found
No related tags found
1 merge request!6Issue #3405229 by partdigital, Odai Atieh: Order of module installation prevents proper access_policy field creation.
Pipeline #60526 passed with warnings
......@@ -19,6 +19,7 @@ use Drupal\Core\Database\Query\SelectInterface;
use Drupal\Core\Entity\EntityFormInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
......@@ -261,6 +262,9 @@ function access_policy_access_policy_insert(EntityInterface $entity) {
\Drupal::service('access_policy.access_policy_data')->clear();
}
// Ensure that the access policy field is installed.
_access_policy_install_access_policy_field($entity->getTargetEntityTypeId());
// Clear field cache so extra field is added or removed.
\Drupal::service('entity_field.manager')->clearCachedFieldDefinitions();
// Clear the views data cache so the extra field is available in views.
......@@ -271,6 +275,38 @@ function access_policy_access_policy_insert(EntityInterface $entity) {
\Drupal::service('access_policy.information')->resetCache();
}
/**
* Install the access policy field if it is missing.
*
* In rare cases the access_policy field might not be properly installed. This
* can happen if an entity becomes compatible with Access policy after that
* entity was originally installed. For example, perhaps a contrib module adds
* bundle support to an entity that did not have it before.
*
* @param string $entity_type_id
* The entity type id.
*
* @see \Drupal\access_policy\EntityTypeInfo::entityBaseFieldInfo()
*/
function _access_policy_install_access_policy_field($entity_type_id) {
$definition_update_manager = \Drupal::entityDefinitionUpdateManager();
$exists = $definition_update_manager->getFieldStorageDefinition('access_policy', $entity_type_id);
if (!$exists) {
$field_manager = \Drupal::service('entity_field.manager');
$schema_repository = \Drupal::service('entity.last_installed_schema.repository');
$field_manager->useCaches(FALSE);
$storage_definitions = $field_manager->getFieldStorageDefinitions($entity_type_id);
$field_manager->useCaches(TRUE);
$installed_storage_definitions = $schema_repository->getLastInstalledFieldStorageDefinitions($entity_type_id);
foreach (array_diff_key($storage_definitions, $installed_storage_definitions) as $storage_definition) {
if ($storage_definition->getProvider() == 'access_policy') {
$definition_update_manager->installFieldStorageDefinition($storage_definition->getName(), $entity_type_id, 'access_policy', $storage_definition);
}
}
}
}
/**
* Implements hook_entity_insert().
*/
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment