MergeRemoteStructure.php in Conflict 8.2
File
modules/conflict_paragraphs/src/ConflictResolution/MergeRemoteStructure.php
View source
<?php
namespace Drupal\conflict_paragraphs\ConflictResolution;
use Drupal\Component\Utility\NestedArray;
use Drupal\conflict\ConflictResolution\MergeStrategyBase;
use Drupal\conflict\Entity\EntityConflictHandlerInterface;
use Drupal\conflict\Event\EntityConflictResolutionEvent;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\paragraphs\Plugin\Field\FieldWidget\ParagraphsWidget;
class MergeRemoteStructure extends MergeStrategyBase {
public function getMergeStrategyId() : string {
return 'conflict.merge_remote_paragraph_structure';
}
public function resolveConflictsContentEntity(EntityConflictResolutionEvent $event) {
$local_entity = $event
->getLocalEntity();
$remote_entity = $event
->getRemoteEntity();
$original_entity = $event
->getBaseEntity();
$result_entity = $event
->getResultEntity();
$form_state = $event
->getContextParameter('form_state');
foreach ($event
->getConflicts() as $property => $conflict_type) {
$field_definition = $remote_entity
->getFieldDefinition($property);
if (ParagraphsWidget::isApplicable($field_definition)) {
$local_paragraph_ids_unsorted = array_map(function ($value) {
return $value['target_id'];
}, $local_entity
->get($property)
->getValue());
$server_paragraph_ids_unsorted = array_map(function ($value) {
return $value['target_id'];
}, $remote_entity
->get($property)
->getValue());
$original_paragraph_ids_unsorted = array_map(function ($value) {
return $value['target_id'];
}, $original_entity
->get($property)
->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);
if ($server_paragraph_ids_sorted == $original_paragraph_ids_sorted) {
$field_item_list = $result_entity
->get($property);
$server_field_item_list = $remote_entity
->get($property);
$field_item_list_unsorted = clone $field_item_list;
$field_item_list
->setValue(NULL);
foreach ($server_field_item_list as $index => $server_field_item) {
$entity_id = $server_field_item->target_id;
foreach ($field_item_list_unsorted as $local_item) {
if ($entity_id == $local_item->target_id) {
if ($local_item->entity->{EntityConflictHandlerInterface::CONFLICT_ENTITY_NEEDS_MANUAL_MERGE}) {
$item_value = $local_item
->getValue();
$item_value['entity'] = $local_item->entity;
}
else {
$item_value = $server_field_item
->getValue();
$item_value['entity'] = $server_field_item->entity;
if ($form_state) {
if (!$form_state
->isValidationComplete()) {
$item_value['entity'] = clone $item_value['entity'];
}
$path = [
$property,
$index,
];
$input =& $form_state
->getUserInput();
NestedArray::unsetValue($input, $path);
}
}
$field_item_list
->appendItem($item_value);
break;
}
}
}
if ($form_state) {
$this
->reorderWidgetState($field_item_list, $form_state);
}
}
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);
if ($new_paragraph_ids && empty($removed_paragraph_ids)) {
$field_item_list = $result_entity
->get($property);
$server_field_item_list = $remote_entity
->get($property);
$field_item_list_unsorted = clone $field_item_list;
$field_item_list
->setValue(NULL);
foreach ($server_paragraph_ids_unsorted as $index => $entity_id) {
foreach ($field_item_list_unsorted as $item) {
if ($entity_id == $item->target_id) {
$item_value = $item
->getValue();
$item_value['entity'] = $item->entity;
$field_item_list
->appendItem($item_value);
continue 2;
}
}
foreach ($server_field_item_list as $item) {
if ($entity_id == $item->target_id) {
$item_value = $item
->getValue();
$item_value['entity'] = $item->entity;
$field_item_list
->appendItem($item_value);
if ($form_state && !$form_state
->isValidationComplete()) {
$item_value['entity'] = clone $item_value['entity'];
}
break;
}
}
}
if ($form_state) {
$this
->reorderWidgetState($field_item_list, $form_state);
}
}
elseif ($removed_paragraph_ids && empty($new_paragraph_ids)) {
$field_item_list = $result_entity
->get($property);
$field_item_list_unsorted = clone $field_item_list;
$field_item_list
->setValue(NULL);
foreach ($field_item_list_unsorted as $item) {
if (!in_array($item->target_id, $removed_paragraph_ids)) {
$item_value = $item
->getValue();
$item_value['entity'] = $item->entity;
$field_item_list
->appendItem($item_value);
}
else {
$entity = $item->entity;
if ($entity->{EntityConflictHandlerInterface::CONFLICT_ENTITY_NEEDS_MANUAL_MERGE} && $entity->{EntityConflictHandlerInterface::CONFLICT_ENTITY_SERVER} === 'removed') {
$item_value = $item
->getValue();
$item_value['entity'] = $item->entity;
$field_item_list
->appendItem($item_value);
}
}
}
if ($form_state) {
$this
->reorderWidgetState($field_item_list, $form_state);
}
}
elseif (!empty($removed_paragraph_ids) && !empty($new_paragraph_ids)) {
if ($form_state) {
$form_state
->set('manual-merge-not-possible', TRUE);
}
}
}
}
}
}
protected function reorderWidgetState(FieldItemListInterface $field_item_list, FormStateInterface $form_state) {
$field_name = $field_item_list
->getName();
$widget_state = ParagraphsWidget::getWidgetState([], $field_name, $form_state);
$old_widget_state_paragraphs = $widget_state['paragraphs'];
$widget_state['paragraphs'] = [];
foreach ($field_item_list as $new_delta => $item) {
$found = FALSE;
foreach ($old_widget_state_paragraphs as $old_delta => $old_delta_state) {
if ($item->target_id == $old_delta_state['entity']
->id()) {
$widget_state['paragraphs'][$new_delta] = $old_delta_state;
$widget_state['paragraphs'][$new_delta]['entity'] = $item->entity;
$found = TRUE;
}
}
if (!$found) {
$widget_state['paragraphs'][$new_delta]['entity'] = $item->entity;
}
}
$widget_state['items_count'] = count($widget_state['paragraphs']);
ParagraphsWidget::setWidgetState([], $field_name, $form_state, $widget_state);
}
}