You are here

protected function FieldComparatorParagraphReference::getConflictTypeForCommonParagraphs in Conflict 8.2

Returns the conflict type for common paragraphs.

Parameters

\Drupal\Core\Field\FieldItemListInterface $local: The local field item list to compare.

\Drupal\Core\Field\FieldItemListInterface $server: The server field item list to compare.

\Drupal\Core\Field\FieldItemListInterface $original: The original field item list, from which local and the server emerged.

Return value

string|null The conflict type or NULL if none.

1 call to FieldComparatorParagraphReference::getConflictTypeForCommonParagraphs()
FieldComparatorParagraphReference::getConflictType in modules/conflict_paragraphs/src/Plugin/Conflict/FieldComparator/FieldComparatorParagraphReference.php
Returns the conflict type for a field.

File

modules/conflict_paragraphs/src/Plugin/Conflict/FieldComparator/FieldComparatorParagraphReference.php, line 143

Class

FieldComparatorParagraphReference
Field comparator plugin implementation for paragraph reference fields.

Namespace

Drupal\conflict_paragraphs\Plugin\Conflict\FieldComparator

Code

protected function getConflictTypeForCommonParagraphs(FieldItemListInterface $local, FieldItemListInterface $server, FieldItemListInterface $original) {
  $conflict_type = NULL;

  /** @var \Drupal\conflict\Entity\EntityConflictHandlerInterface $entity_conflict_resolution_handler */
  $entity_conflict_resolution_handler = $this->entityTypeManager
    ->getHandler('paragraph', 'conflict.resolution_handler');
  foreach ($local as $local_item) {
    $local_item_target_id = $local_item->target_id;

    /** @var \Drupal\paragraphs\ParagraphInterface $local_item_entity */
    $local_item_entity = $local_item->entity;

    /** @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface $local_item_entity_form_display */
    $local_item_entity_form_display = $local_item_entity->{EntityConflictHandlerInterface::CONFLICT_FORM_DISPLAY};
    $server_item_entity = NULL;
    foreach ($server as $server_item) {
      if ($local_item_target_id == $server_item->target_id) {

        /** @var \Drupal\paragraphs\ParagraphInterface $server_item_entity */
        $server_item_entity = $server_item->entity;
        break;
      }
    }
    foreach ($original as $original_item) {
      if ($local_item_target_id == $original_item->target_id) {

        /** @var \Drupal\paragraphs\ParagraphInterface $original_item_entity */
        $original_item_entity = $original_item->entity;
        break;
      }
    }
    if ($server_item_entity) {
      foreach (array_keys($local_item_entity_form_display
        ->getComponents()) as $paragraph_field_name) {
        $local_paragraph_field = $local_item_entity
          ->get($paragraph_field_name);
        $server_paragraph_field = $server_item_entity
          ->get($paragraph_field_name);
        if (!$server_paragraph_field
          ->equals($local_paragraph_field)) {
          $original_paragraph_field = $original_item_entity
            ->get($paragraph_field_name);
          if (!$server_paragraph_field
            ->equals($original_paragraph_field)) {
            if (!$local_paragraph_field
              ->equals($original_paragraph_field)) {
              $conflict_type = ConflictTypes::CONFLICT_TYPE_LOCAL_REMOTE;

              // TODO find a better place to append the server entity. This
              // is needed for preparation of the conflict resolution.
              // @see \Drupal\conflict\Entity\ContentEntityConflictHandler::prepareConflictResolution()
              // Maybe the Merge Strategy is a better suited place?
              $entity_conflict_resolution_handler
                ->prepareConflictResolution($local_item_entity, $server_item_entity);
              break;
            }
          }
        }
      }
    }
    else {
      foreach (array_keys($local_item_entity_form_display
        ->getComponents()) as $paragraph_field_name) {
        $local_paragraph_field = $local_item_entity
          ->get($paragraph_field_name);
        $original_paragraph_field = $original_item_entity
          ->get($paragraph_field_name);
        if (!$local_paragraph_field
          ->equals($original_paragraph_field)) {
          $conflict_type = ConflictTypes::CONFLICT_TYPE_LOCAL_REMOTE;

          // TODO find a better place to append the server entity. This
          // is needed for preparation of the conflict resolution.
          // @see \Drupal\conflict\Entity\ContentEntityConflictHandler::prepareConflictResolution()
          // Maybe the Merge Strategy is a better suited place?
          $entity_conflict_resolution_handler
            ->prepareConflictResolution($local_item_entity, $server_item_entity);
          break;
        }
      }
    }
  }
  $local_paragraph_ids_unsorted = array_map(function ($value) {
    return $value['target_id'];
  }, $local
    ->getValue());
  $original_paragraph_ids_unsorted = array_map(function ($value) {
    return $value['target_id'];
  }, $original
    ->getValue());
  $removed_locally_paragraph_ids = array_diff($original_paragraph_ids_unsorted, $local_paragraph_ids_unsorted);
  foreach ($removed_locally_paragraph_ids as $paragraph_id) {
    $original_item_entity = NULL;
    foreach ($original as $original_item) {
      if ($paragraph_id == $original_item->target_id) {

        /** @var \Drupal\paragraphs\ParagraphInterface $original_item_entity */
        $original_item_entity = $original_item->entity;
        break;
      }
    }
    $server_item_entity = NULL;
    foreach ($server as $server_item) {
      if ($paragraph_id == $server_item->target_id) {

        /** @var \Drupal\paragraphs\ParagraphInterface $original_item_entity */
        $server_item_entity = $server_item->entity;
        break;
      }
    }

    /** @var \Drupal\Core\Field\WidgetInterface $widget */
    $widget = $local->conflictWidget;
    if ($widget) {
      $removed_entity_form_display = EntityFormDisplay::collectRenderDisplay($original_item_entity, $widget
        ->getSetting('form_display_mode'));
    }
    else {

      // TODO
    }
    foreach (array_keys($removed_entity_form_display
      ->getComponents()) as $paragraph_field_name) {
      $server_paragraph_field = $server_item_entity
        ->get($paragraph_field_name);
      $original_paragraph_field = $original_item_entity
        ->get($paragraph_field_name);
      if (!$server_paragraph_field
        ->equals($original_paragraph_field)) {
        $conflicting_removed_paragraph_ids = $local->conflictingRemovedParagraphIds ?? [];
        $conflicting_removed_paragraph_ids[] = $paragraph_id;
        $local->conflictingRemovedParagraphIds = $conflicting_removed_paragraph_ids;
        $conflict_type = ConflictTypes::CONFLICT_TYPE_LOCAL_REMOTE;
        break;
      }
    }
  }
  return $conflict_type;
}