You are here

public function FieldComparatorParagraphReference::getConflictType in Conflict 8.2

Returns the conflict type for a field.

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.

string $langcode: The language code of the entity translation being checked.

string $entity_type_id: The entity type ID.

string $bundle: The entity bundle ID.

string $field_type: The field type.

string $field_name: The field name.

Return value

string|null The conflict type or NULL if none.

Throws

\Exception An exception will be thrown if for some reason even the default field comparator has not been added to the field comparators list.

Overrides FieldComparatorDefault::getConflictType

File

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

Class

FieldComparatorParagraphReference
Field comparator plugin implementation for paragraph reference fields.

Namespace

Drupal\conflict_paragraphs\Plugin\Conflict\FieldComparator

Code

public function getConflictType(FieldItemListInterface $local, FieldItemListInterface $server, FieldItemListInterface $original, $langcode, $entity_type_id, $bundle, $field_type, $field_name) {

  /** @var \Drupal\entity_reference_revisions\EntityReferenceRevisionsFieldItemList $local */

  /** @var \Drupal\entity_reference_revisions\EntityReferenceRevisionsFieldItemList $server */

  /** @var \Drupal\entity_reference_revisions\EntityReferenceRevisionsFieldItemList $original */

  // If the default conflict detection is detecting only a remote change then
  // we need to compare exactly the referenced entities in order to detect
  // whether there has been only remote change or also a local conflicting
  // change.
  $conflict_type = parent::getConflictType($local, $server, $original, $langcode, $entity_type_id, $bundle, $field_type, $field_name);
  if (ConflictTypes::CONFLICT_TYPE_REMOTE === $conflict_type) {
    $local_paragraph_ids_unsorted = array_map(function ($value) {
      return $value['target_id'];
    }, $local
      ->getValue());
    $server_paragraph_ids_unsorted = array_map(function ($value) {
      return $value['target_id'];
    }, $server
      ->getValue());
    $original_paragraph_ids_unsorted = array_map(function ($value) {
      return $value['target_id'];
    }, $original
      ->getValue());
    $local_paragraph_ids_sorted = $local_paragraph_ids_unsorted;
    $server_paragraph_ids_sorted = $server_paragraph_ids_unsorted;
    $original_paragraph_ids_sorted = $original_paragraph_ids_unsorted;
    sort($local_paragraph_ids_sorted);
    sort($server_paragraph_ids_sorted);
    sort($original_paragraph_ids_sorted);

    // No entities have been added or deleted in the server version.
    if ($server_paragraph_ids_sorted == $original_paragraph_ids_sorted) {
      $conflict_type = $this
        ->getConflictTypeForCommonParagraphs($local, $server, $original) ?: $conflict_type;
    }
    else {
      $new_paragraph_ids = array_diff($server_paragraph_ids_sorted, $original_paragraph_ids_sorted);
      $removed_paragraph_ids = array_diff($original_paragraph_ids_sorted, $server_paragraph_ids_sorted);

      // Only new paragraphs added.
      if ($new_paragraph_ids && empty($removed_paragraph_ids)) {

        // TODO check whether common paragraphs have been changed in remote
        // and local. If not classify as REMOTE_ONLY changes - because of the
        // added paragraphs.
        $conflict_type = $this
          ->getConflictTypeForCommonParagraphs($local, $server, $original) ?: $conflict_type;
      }
      elseif ($removed_paragraph_ids && empty($new_paragraph_ids)) {

        // TODO not yet supported.
        // Check that paragraphs removed from server haven't changed in local
        // version compared to original and that common paragraphs haven't
        // changed as well. If both conditions are fulfilled classify as
        // REMOTE only change. If however one of both conditions is not
        // fulfilled classify as Remote-Local conflict.
        $conflict_type = $this
          ->getConflictTypeForCommonParagraphs($local, $server, $original) ?: $conflict_type;
      }
      else {

        // TODO not yet supported.
        // Remote only change if:
        //   1. Check that paragraphs removed from server haven't changed in
        //      local version compared to original.
        //   2. Common paragraphs haven't changed as well.
        // Remote and Local conlict: if one of both conditions is not
        // fulfilled.
        $conflict_type = $this
          ->getConflictTypeForCommonParagraphs($local, $server, $original) ?: $conflict_type;
      }
    }
  }
  return $conflict_type;
}