You are here

protected function UnsavedConfigurationFormTrait::checkEntityEditable in Search API 8

Checks whether the given entity contains unsaved changes.

If this is the case and the changes were made by a different user, the form is disabled and a message displayed.

Optionally, if there are unsaved changes by the current user, a different message can be displayed.

Parameters

array $form: The form structure, passed by reference.

object $entity: The entity in question.

bool $reportChanged: (optional) If TRUE, also show a message for unsaved changes by the current user.

3 calls to UnsavedConfigurationFormTrait::checkEntityEditable()
FieldConfigurationForm::form in src/Form/FieldConfigurationForm.php
Gets the actual form array to be built.
IndexAddFieldsForm::buildForm in src/Form/IndexAddFieldsForm.php
Form constructor.
IndexFieldsForm::buildForm in src/Form/IndexFieldsForm.php
Form constructor.

File

src/Form/UnsavedConfigurationFormTrait.php, line 63

Class

UnsavedConfigurationFormTrait
Provides a helper methods for forms to correctly treat unsaved configuration.

Namespace

Drupal\search_api\Form

Code

protected function checkEntityEditable(array &$form, $entity, $reportChanged = FALSE) {
  if ($entity instanceof UnsavedConfigurationInterface && $entity
    ->hasChanges()) {
    if ($entity
      ->isLocked()) {
      $form['#disabled'] = TRUE;
      $username = [
        '#theme' => 'username',
        '#account' => $entity
          ->getLockOwner(),
      ];
      $lockMessageSubstitutions = [
        '@user' => $this->renderer
          ->render($username),
        '@age' => $this->dateFormatter
          ->formatTimeDiffSince($entity
          ->getLastUpdated()),
        ':url' => $entity
          ->toUrl('break-lock-form')
          ->toString(),
      ];
      $form['locked'] = [
        '#type' => 'container',
        '#attributes' => [
          'class' => [
            'index-locked',
            'messages',
            'messages--warning',
          ],
        ],
        '#children' => $this
          ->t('This index is being edited by user @user, and is therefore locked from editing by others. This lock is @age old. Click here to <a href=":url">break this lock</a>.', $lockMessageSubstitutions),
        '#weight' => -10,
      ];
    }
    elseif ($reportChanged) {
      $form['changed'] = [
        '#type' => 'container',
        '#attributes' => [
          'class' => [
            'index-changed',
            'messages',
            'messages--warning',
          ],
        ],
        '#children' => $this
          ->t('You have unsaved changes.'),
        '#weight' => -10,
      ];
    }
  }
}