View source
<?php
namespace Drupal\conflict_paragraphs\Plugin\Conflict\FieldComparator;
use Drupal\conflict\ConflictTypes;
use Drupal\conflict\Entity\EntityConflictHandlerInterface;
use Drupal\conflict\Plugin\Conflict\FieldComparator\FieldComparatorDefault;
use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\paragraphs\Plugin\Field\FieldWidget\ParagraphsWidget;
use Symfony\Component\DependencyInjection\ContainerInterface;
class FieldComparatorParagraphReference extends FieldComparatorDefault implements ContainerFactoryPluginInterface {
protected $entityTypeManager;
public function __construct($plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager) {
parent::__construct([], $plugin_id, $plugin_definition);
$this->entityTypeManager = $entity_type_manager;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($plugin_id, $plugin_definition, $container
->get('entity_type.manager'));
}
public function getConflictType(FieldItemListInterface $local, FieldItemListInterface $server, FieldItemListInterface $original, $langcode, $entity_type_id, $bundle, $field_type, $field_name) {
$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);
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);
if ($new_paragraph_ids && empty($removed_paragraph_ids)) {
$conflict_type = $this
->getConflictTypeForCommonParagraphs($local, $server, $original) ?: $conflict_type;
}
elseif ($removed_paragraph_ids && empty($new_paragraph_ids)) {
$conflict_type = $this
->getConflictTypeForCommonParagraphs($local, $server, $original) ?: $conflict_type;
}
else {
$conflict_type = $this
->getConflictTypeForCommonParagraphs($local, $server, $original) ?: $conflict_type;
}
}
}
return $conflict_type;
}
protected function getConflictTypeForCommonParagraphs(FieldItemListInterface $local, FieldItemListInterface $server, FieldItemListInterface $original) {
$conflict_type = NULL;
$entity_conflict_resolution_handler = $this->entityTypeManager
->getHandler('paragraph', 'conflict.resolution_handler');
foreach ($local as $local_item) {
$local_item_target_id = $local_item->target_id;
$local_item_entity = $local_item->entity;
$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) {
$server_item_entity = $server_item->entity;
break;
}
}
foreach ($original as $original_item) {
if ($local_item_target_id == $original_item->target_id) {
$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;
$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;
$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) {
$original_item_entity = $original_item->entity;
break;
}
}
$server_item_entity = NULL;
foreach ($server as $server_item) {
if ($paragraph_id == $server_item->target_id) {
$server_item_entity = $server_item->entity;
break;
}
}
$widget = $local->conflictWidget;
if ($widget) {
$removed_entity_form_display = EntityFormDisplay::collectRenderDisplay($original_item_entity, $widget
->getSetting('form_display_mode'));
}
else {
}
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;
}
public static function isApplicable(FieldDefinitionInterface $field_definition) {
return ParagraphsWidget::isApplicable($field_definition);
}
}