You are here

function conflict_prepare_entity_form in Conflict 8.2

Helper method for preparing entity forms for conflict resolution.

The entity is present as a parameter to support inline entity forms.

2 calls to conflict_prepare_entity_form()
conflict_form_alter in ./conflict.module
Implements hook_form_alter().
conflict_paragraphs_field_widget_paragraphs_form_alter in modules/conflict_paragraphs/conflict_paragraphs.module
Implements hook_field_widget_WIDGET_TYPE_form_alter().

File

./conflict.module, line 141
The module that makes concurrent editing possible.

Code

function conflict_prepare_entity_form(&$form, FormStateInterface $form_state, EntityInterface $entity, $inline_entity_form = FALSE) {
  $conflict_supported = $form_state
    ->get('conflict.supported');
  if ($conflict_supported === FALSE) {
    return;
  }
  elseif (is_null($conflict_supported)) {
    $route = \Drupal::routeMatch()
      ->getRouteObject();
    if (!($route && ($route_defaults = $route
      ->getDefaults()) && isset($route_defaults['_entity_form']))) {
      $form_state
        ->set('conflict.supported', FALSE);
      return;
    }
    elseif ($entity instanceof RevisionableInterface && (!$entity
      ->isDefaultRevision() && !(bool) $form_state
      ->get('conflict-exchanged-entity'))) {
      $form_state
        ->set('conflict.supported', FALSE);
      return;
    }

    // Flags the form that on it a conflict resolution is supported.
    $form_state
      ->set('conflict.supported', TRUE);
  }
  $entity_type_id = $entity
    ->getEntityTypeId();
  $bundle = $entity
    ->bundle();
  $entity_type_manager = \Drupal::entityTypeManager();
  if ($entity_type_manager
    ->hasHandler($entity_type_id, 'conflict.resolution_handler')) {
    if (!$inline_entity_form) {

      /** @var \Drupal\Core\Entity\ContentEntityFormInterface $form_object */
      $form_object = $form_state
        ->getFormObject();
      $entity->{EntityConflictHandlerInterface::CONFLICT_FORM_DISPLAY} = $form_object
        ->getFormDisplay($form_state);
    }

    /** @var \Drupal\conflict\Entity\EntityConflictHandlerInterface $entity_conflict_resolution_handler */
    $entity_conflict_resolution_handler = $entity_type_manager
      ->getHandler($entity_type_id, 'conflict.resolution_handler');
    $entity_conflict_resolution_handler
      ->entityFormAlter($form, $form_state, $entity, $inline_entity_form);

    // Retrieve the resolution strategy from the settings and if none selected
    // default to inline.
    $settings = \Drupal::configFactory()
      ->get('conflict.settings');
    $strategy = $settings
      ->get("resolution_type.{$entity_type_id}.{$bundle}") ?? $settings
      ->get("resolution_type.{$entity_type_id}.default") ?? $settings
      ->get("resolution_type.default.default") ?? 'inline';
    if ($strategy === 'dialog') {

      // Add the dialog conflict resolution overview only to the main entity
      // form and not to the nested entity forms.
      if (!$inline_entity_form && ($form_state
        ->get('conflict.build_conflict_resolution_form') || $form_state
        ->get('conflict.processing'))) {
        \Drupal::service('conflict.resolution_form.builder')
          ->processForm($form, $form_state);
      }
    }
    else {
      if ($form_state
        ->get('conflict.build_conflict_resolution_form')) {
        \Drupal::service('conflict.resolution_inline_form.builder')
          ->processForm($form, $form_state, $entity);
      }
    }
  }
}