You are here

function cms_content_sync_form_alter in CMS Content Sync 2.0.x

Same name and namespace in other branches
  1. 8 cms_content_sync.module \cms_content_sync_form_alter()
  2. 2.1.x cms_content_sync.module \cms_content_sync_form_alter()

1) Make sure the user is informed that content will not only be deleted on this * instance but also on all connected instances if configured that way.

2) Make sure Sync Core knows about password changes at the Content Sync user and can still authenticate to perform updates.

3) Disabled node forms if the content has been pulled and the synchronization is configured to disable pulled content.

Parameters

array $form: The form definition.

\Drupal\Core\Form\FormStateInterface $form_state: The form state.

string $form_id: he ID of the form.

Throws

Exception

See also

_cms_content_sync_form_alter_disabled_fields

File

./cms_content_sync.module, line 349
Module file for cms_content_sync.

Code

function cms_content_sync_form_alter(array &$form, FormStateInterface $form_state, $form_id) {
  if (!_cms_content_sync_is_installed()) {
    return;
  }
  if ($form_id == 'taxonomy_overview_terms') {
    $form['#validate'][] = 'cms_content_sync_update_taxonomy_tree_validate';
    $form['#submit'][] = 'cms_content_sync_update_taxonomy_tree_submit';
  }
  $form_object = $form_state
    ->getFormObject();

  // Avoid function nesting error for conditional fields.
  // @todo Find a way to limit this function call in a useful way.
  if ($form_id != 'conditional_field_edit_form') {
    _cms_content_sync_add_embedded_entity_submit_handler($form, $form);
  }
  if ($form_id === 'user_form') {
    $form['actions']['submit']['#submit'][] = 'cms_content_sync_user_password_submit';
  }
  $webform = FALSE;
  $moduleHandler = Drupal::service('module_handler');
  if ($moduleHandler
    ->moduleExists('webform')) {
    if ($form_object instanceof WebformUiEntityElementsForm) {
      $webform = TRUE;
    }
  }
  if ($form_object instanceof DeleteMultiple || $form_object instanceof ContentEntityDeleteForm) {
    if (!empty($form_state
      ->getUserInput()['confirm'])) {
      return;
    }
    if ($form_object instanceof DeleteMultiple) {
      $temp_store_factory = Drupal::service('tempstore.private');
      $entity_type_manager = Drupal::service('entity_type.manager');
      $tempstore = $temp_store_factory
        ->get('entity_delete_multiple_confirm');
      $user = Drupal::currentUser();

      // @todo Extend this that it is also working with other entity types.
      $entity_type_id = 'node';
      $selection = $tempstore
        ->get($user
        ->id() . ':' . $entity_type_id);
      $entities = $entity_type_manager
        ->getStorage($entity_type_id)
        ->loadMultiple(array_keys($selection));
    }
    else {
      $entities[] = $form_object
        ->getEntity();
    }
    foreach ($entities as $entity) {
      if (!EntityHandlerPluginManager::isSupported($entity
        ->getEntityTypeId(), $entity
        ->bundle())) {
        continue;
      }
      if (!Flow::isLocalDeletionAllowed($entity)) {
        $messenger = Drupal::messenger();
        $messenger
          ->addWarning(t('%label cannot be deleted as it has been pulled.', [
          '%label' => $entity
            ->label(),
        ]));

        // ['actions']['submit'].
        $form['#disabled'] = TRUE;
      }
      else {
        $flows = Flow::getFlowsForPushing($entity, SyncIntent::ACTION_DELETE);
        if (count($flows)) {
          $messenger = Drupal::messenger();
          $usage = _cms_content_sync_display_pool_usage($entity);
          $message = $usage ? t('This will delete %label from all sites using it: @sites', [
            '%label' => $entity
              ->label(),
            '@sites' => Markup::create($usage),
          ]) : t('This will delete %label from all sites using it.', [
            '%label' => $entity
              ->label(),
          ]);
          $messenger
            ->addWarning($message);
        }
      }
    }
  }
  elseif ($form_object instanceof ContentEntityForm || $webform) {
    $entity = $form_object
      ->getEntity();
    if (!EntityHandlerPluginManager::isSupported($entity
      ->getEntityTypeId(), $entity
      ->bundle())) {
      return;
    }
    $form['#attached']['library'][] = 'cms_content_sync/entity-form';
    _cms_content_sync_form_alter_disabled_fields($form, $form_state, $entity);
    $bundle = $entity
      ->bundle();
    $selectable_pushing_flows = Pool::getSelectablePools($entity
      ->getEntityTypeId(), $bundle);
    $flows = Flow::getAll();
    if (!empty($selectable_pushing_flows)) {
      _cms_content_sync_add_push_pool_form($form, $selectable_pushing_flows, $entity);
      _cms_content_sync_add_usage_form($form, $entity);
    }
    else {
      $flows = Flow::getFlowsForPushing($entity, SyncIntent::ACTION_DELETE);
      if (count($flows)) {
        _cms_content_sync_add_usage_form($form, $entity);
      }
      else {
        $flows = Flow::getFlowsForPushing($entity, SyncIntent::ACTION_UPDATE);
        if (count($flows)) {
          _cms_content_sync_add_usage_form($form, $entity);
        }
      }
    }
    foreach ($flows as $flow) {
      if ($flow
        ->supportsEntity($entity)) {
        _cms_content_sync_add_version_mismatches_form($form, $form_state);
        _cms_content_sync_add_form_value_cache($form, $form_state);
        break;
      }
    }
    if (_prevent_entity_export($entity)) {
      return;
    }
    $user = Drupal::currentUser();
    if ($user
      ->hasPermission('publish cms content sync changes')) {
      foreach (Flow::getAll() as $flow_id => $flow) {

        // Add "Save and push" button to entity types which are configured to
        // be pushed manually.
        // @todo Show message when an entity is not pushed due to: Not published or no pool selected.
        if ($flow
          ->canPushEntity($entity, PushIntent::PUSH_MANUALLY)) {
          _cms_content_sync_add_save_push_action($form, $form_state, $flow_id, $entity);
          break;
        }

        // Adjust save button label if the entity will be pushed
        // automatically after saving it.
        if ($flow
          ->canPushEntity($entity, PushIntent::PUSH_AUTOMATICALLY)) {
          $form['actions']['submit']['#value'] = t('Save and push');
          break;
        }
      }
    }
  }
}