You are here

class ConflictResolutionInlineFormBuilder in Conflict 8.2

Hierarchy

Expanded class hierarchy of ConflictResolutionInlineFormBuilder

1 string reference to 'ConflictResolutionInlineFormBuilder'
conflict.services.yml in ./conflict.services.yml
conflict.services.yml
1 service uses ConflictResolutionInlineFormBuilder
conflict.resolution_inline_form.builder in ./conflict.services.yml
Drupal\conflict\Form\ConflictResolutionInlineFormBuilder

File

src/Form/ConflictResolutionInlineFormBuilder.php, line 16

Namespace

Drupal\conflict\Form
View source
class ConflictResolutionInlineFormBuilder {
  use DependencySerializationTrait;
  use StringTranslationTrait;

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * The module handler service.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected $moduleHandler;

  /**
   * ConflictResolutionFormBuilder constructor.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   The module handler
   * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
   *   The string translation service.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager, ModuleHandlerInterface $module_handler, TranslationInterface $string_translation) {
    $this->entityTypeManager = $entity_type_manager;
    $this->moduleHandler = $module_handler;
    $this->stringTranslation = $string_translation;
  }

  /**
   * Adds the conflict resolution overview to the form.
   *
   * @param $form
   *   The form array.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The current state of the form.
   * @param \Drupal\Core\Entity\EntityInterface $entity
   *   The entity of the form.
   */
  public function processForm(&$form, FormStateInterface $form_state, EntityInterface $entity) {
    if (!$entity instanceof ContentEntityInterface) {
      return;
    }

    // If the entity has not been flagged for manual merge then no need to
    // proceed here.
    // @see \Drupal\conflict\Entity\ContentEntityConflictHandler::prepareConflictResolution()
    if (!$entity->{EntityConflictHandlerInterface::CONFLICT_ENTITY_NEEDS_MANUAL_MERGE}) {
      return;
    }

    /** @var \Drupal\conflict\Entity\EntityConflictHandlerInterface $entity_conflict_resolution_handler */
    $entity_conflict_resolution_handler = $this->entityTypeManager
      ->getHandler($entity
      ->getEntityTypeId(), 'conflict.resolution_handler');

    /** @var \Drupal\Core\Entity\ContentEntityInterface $entity_local_original */
    $entity_local_original = $entity->{EntityConflictHandlerInterface::CONFLICT_ENTITY_ORIGINAL};
    $entity_server = $entity->{EntityConflictHandlerInterface::CONFLICT_ENTITY_SERVER};
    $conflicts = [];
    if ($entity_server === 'removed') {
      $form['conflict_resolution_confirm_removed'] = [
        '#type' => 'checkbox',
        '#required' => TRUE,
        '#title' => $this
          ->t('This %entity_type has been removed. Confirm if you want to keep it or remove it.', [
          '%entity_type' => $entity_local_original
            ->getEntityType()
            ->getSingularLabel(),
        ]),
      ];
    }
    else {
      $conflicts = $entity_conflict_resolution_handler
        ->getConflicts($entity_local_original, $entity, $entity_server);
    }
    foreach ($conflicts as $field_name => $conflict_type) {
      $form[$field_name]['conflict_resolution'] = [
        '#type' => 'details',
        '#title' => $entity
          ->get($field_name)
          ->getFieldDefinition()
          ->getLabel() . ' - ' . $this
          ->t('Conflict resolution'),
        '#open' => TRUE,
      ];
      $form[$field_name]['conflict_resolution']['overview'] = [
        '#type' => 'table',
        '#header' => [
          $this
            ->t('Local version'),
          $this
            ->t('Initial version'),
          $this
            ->t('Server version'),
        ],
        '#rows' => [
          [
            [
              'data' => $entity
                ->get($field_name)
                ->view(),
            ],
            [
              'data' => $entity_local_original
                ->get($field_name)
                ->view(),
            ],
            [
              'data' => $entity_server
                ->get($field_name)
                ->view(),
            ],
          ],
        ],
      ];
      $form[$field_name]['conflict_resolution']['confirm'] = [
        '#type' => 'checkbox',
        '#required' => TRUE,
        '#title' => $this
          ->t('Manual merge completed'),
      ];
    }
    foreach ($conflicts as $field_name => &$conflict_type) {
      $conflict_type = [
        'conflict-type' => $conflict_type,
      ];
    }
    $manual_merge_conflicts = $form_state
      ->get('manual-merge-conflicts');
    if ($manual_merge_conflicts === NULL) {
      $form_state
        ->set('manual-merge-conflicts', []);
      $manual_merge_conflicts = $form_state
        ->get('manual-merge-conflicts');
    }
    $path_to_entity = $form['#parents'];
    array_pop($path_to_entity);
    $conflicts_with_path = [];
    NestedArray::setValue($conflicts_with_path, $path_to_entity, $conflicts);
    $manual_merge_conflicts = array_merge_recursive($manual_merge_conflicts, $conflicts_with_path);
    $form_state
      ->set('manual-merge-conflicts', $manual_merge_conflicts);
    $this->entityTypeManager
      ->getHandler($entity
      ->getEntityTypeId(), 'conflict.resolution_handler')
      ->finishConflictResolution($entity, [], $form_state);

    // Ensure the form will not be flagged for rebuild.
    // @see \Drupal\conflict\Entity\ContentEntityConflictHandler::entityMainFormValidateLast().
    $form_state
      ->set('conflict.paths', []);
    $message = $this
      ->t('The content has either been modified by another user, or you have already submitted modifications. Manual merge of the conflicts is required.');
    $form['#attached']['drupalSettings']['conflict']['inlineResolutionMessage'] = (string) $message;
    $form['#attached']['library'][] = 'conflict/drupal.conflict_resolution';
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConflictResolutionInlineFormBuilder::$entityTypeManager protected property The entity type manager.
ConflictResolutionInlineFormBuilder::$moduleHandler protected property The module handler service.
ConflictResolutionInlineFormBuilder::processForm public function Adds the conflict resolution overview to the form.
ConflictResolutionInlineFormBuilder::__construct public function ConflictResolutionFormBuilder constructor.
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.