You are here

function content_lock_form_alter in Content locking (anti-concurrent editing) 8

Same name and namespace in other branches
  1. 8.2 content_lock.module \content_lock_form_alter()
  2. 6.2 content_lock.module \content_lock_form_alter()
  3. 6 content_lock.module \content_lock_form_alter()
  4. 7.3 content_lock.module \content_lock_form_alter()
  5. 7 content_lock.module \content_lock_form_alter()
  6. 7.2 content_lock.module \content_lock_form_alter()

Implements hook_form_FORM_ID_alter().

File

./content_lock.module, line 62
Content lock - Main functions of the module.

Code

function content_lock_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  if (!$form_state
    ->getFormObject() instanceof EntityFormInterface) {
    return;
  }

  /** @var \Drupal\core\Entity\ContentEntityInterface $entity */
  $entity = $form_state
    ->getFormObject()
    ->getEntity();
  $entity_type = $entity
    ->getEntityTypeId();
  $user = Drupal::currentUser();

  // Check if we must lock this entity.

  /** @var \Drupal\content_lock\ContentLock\ContentLock $lock_service */
  $lock_service = \Drupal::service('content_lock');
  $form_op = $form_state
    ->getFormObject()
    ->getOperation();
  if (!$lock_service
    ->isLockable($entity, $form_op)) {
    return;
  }

  // We act only on edit form, not for a creation of a new entity.
  if (!is_null($entity
    ->id())) {
    foreach ([
      'submit',
      'publish',
    ] as $key) {
      if (isset($form['actions'][$key])) {
        $form['actions'][$key]['#submit'][] = 'content_lock_form_submit';
      }
    }

    // This hook function is called twice, first when the form loads
    // and second when the form is submitted.
    // Only perform set and check for lock on initial form load.
    $userInput = $form_state
      ->getUserInput();
    if (!empty($userInput)) {
      return;
    }
    if ($lock_service
      ->isJsLock($entity_type)) {
      $form['#attached']['library'][] = 'content_lock/drupal.content_lock.lock_form';
      $form['#attached']['drupalSettings']['content_lock'] = [
        Html::cleanCssIdentifier($form_id) => [
          'lockUrl' => Url::fromRoute('content_lock.create_lock.' . $entity_type, [
            'entity' => $entity
              ->id(),
            'langcode' => $entity
              ->language()
              ->getId(),
            'form_op' => $form_op,
          ], [
            'query' => [
              'destination' => Drupal::request()
                ->getRequestUri(),
            ],
          ])
            ->toString(),
        ],
      ];
      $form['actions']['#attributes']['class'][] = 'content-lock-actions';

      // If moderation state is in use also disable corresponding buttons.
      if (isset($form['moderation_state'])) {
        $form['moderation_state']['#attributes']['class'][] = 'content-lock-actions';
      }
      return;
    }

    // We lock the content if it is currently edited by another user.
    if (!$lock_service
      ->locking($entity
      ->id(), $entity
      ->language()
      ->getId(), $form_op, $user
      ->id(), $entity_type)) {
      $form['#disabled'] = TRUE;

      // Do not allow deletion, publishing, or unpublishing if locked.
      foreach ([
        'delete',
        'publish',
        'unpublish',
      ] as $key) {
        if (isset($form['actions'][$key])) {
          unset($form['actions'][$key]);
        }
      }

      // If moderation state is in use also disable corresponding buttons.
      if (isset($form['moderation_state'])) {
        unset($form['moderation_state']);
      }
    }
    else {

      // ContentLock::locking() returns TRUE if the content is locked by the
      // current user. Add an unlock button only for this user.
      $form['actions']['unlock'] = $lock_service
        ->unlockButton($entity_type, $entity
        ->id(), $entity
        ->language()
        ->getId(), $form_op, \Drupal::request()->query
        ->get('destination'));
    }
  }
}