You are here

function pbf_field_config_update in Permissions by field 8

Implements hook_ENTITY_TYPE_update().

File

./pbf.module, line 289
Contains pbf.module.

Code

function pbf_field_config_update(FieldConfigInterface $entity) {
  if ($entity
    ->getType() == 'pbf') {
    $original = $entity->original;
    $entityTypeManager = \Drupal::entityTypeManager()
      ->getStorage('field_config');

    // If no change on the field synchronized, and if there is a field selected,
    // we must care about the synchronized_from_target option.
    if ($entity
      ->getSetting('synchronized_with') == $original
      ->getSetting('synchronized_with') && $entity
      ->getSetting('synchronized_with')) {
      if ($entity
        ->getSetting('synchronized_from_target') !== $original
        ->getSetting('synchronized_from_target')) {
        $field_to_synchronize = $entityTypeManager
          ->load($entity
          ->getSetting('synchronized_with'));
        if ($field_to_synchronize) {
          $field_to_synchronize
            ->setSetting('synchronized_from_target', $entity
            ->getSetting('synchronized_from_target'))
            ->save();
        }
      }
      else {
        return;
      }
    }

    // If a new field is selected and there is no synchronization before.
    if ($entity
      ->getSetting('synchronized_with') && empty($original
      ->getSetting('synchronized_with'))) {

      /** @var \Drupal\field\FieldConfigInterface $field_to_synchronize */
      $field_to_synchronize = $entityTypeManager
        ->load($entity
        ->getSetting('synchronized_with'));
      if ($field_to_synchronize) {
        $field_to_synchronize
          ->setSetting('synchronized_by', $entity
          ->id())
          ->setSetting('synchronized_from_target', $entity
          ->getSetting('synchronized_from_target'))
          ->save();
      }
    }

    // If the field selected is removed and no synchronization is enabled.
    if (empty($entity
      ->getSetting('synchronized_with')) && $original
      ->getSetting('synchronized_with')) {

      /** @var \Drupal\field\FieldConfigInterface $field_to_synchronize */
      $field_to_synchronize = $entityTypeManager
        ->load($original
        ->getSetting('synchronized_with'));
      if ($field_to_synchronize) {
        $field_to_synchronize
          ->setSetting('synchronized_by', '')
          ->setSetting('synchronized_from_target', 0)
          ->save();
      }
    }

    // If a new field is selected and there is another field selected before.
    if ($entity
      ->getSetting('synchronized_with') && $original
      ->getSetting('synchronized_with') && $entity
      ->getSetting('synchronized_with') !== $original
      ->getSetting('synchronized_with')) {

      /** @var \Drupal\field\FieldConfigInterface $new_field_to_synchronize */
      $new_field_to_synchronize = $entityTypeManager
        ->load($entity
        ->getSetting('synchronized_with'));
      if ($new_field_to_synchronize) {
        $new_field_to_synchronize
          ->setSetting('synchronized_by', $entity
          ->id())
          ->setSetting('synchronized_from_target', $entity
          ->getSetting('synchronized_from_target'))
          ->save();
      }
      $old_field_to_synchronize = $entityTypeManager
        ->load($original
        ->getSetting('synchronized_with'));
      if ($old_field_to_synchronize) {
        $old_field_to_synchronize
          ->setSetting('synchronized_by', '')
          ->setSetting('synchronized_from_target', 0)
          ->save();
      }
    }
  }
}