You are here

public function ReadOnlyFormSubscriber::onFormAlter in Configuration Read-only mode 8

File

src/EventSubscriber/ReadOnlyFormSubscriber.php, line 45

Class

ReadOnlyFormSubscriber
Check if the given form should be read-only.

Namespace

Drupal\config_readonly\EventSubscriber

Code

public function onFormAlter(ReadOnlyFormEvent $event) {

  // Check if the form is a ConfigFormBase or a ConfigEntityListBuilder.
  $form_object = $event
    ->getFormState()
    ->getFormObject();
  $mark_form_read_only = $form_object instanceof ConfigFormBase || $form_object instanceof ConfigEntityListBuilder;
  if (!$mark_form_read_only) {
    $mark_form_read_only = in_array($form_object
      ->getFormId(), $this->readOnlyFormIds);
  }

  // Check if the form is an EntityFormInterface and entity is a config
  // entity.
  if (!$mark_form_read_only && $form_object instanceof EntityFormInterface) {
    $entity = $form_object
      ->getEntity();
    $mark_form_read_only = $entity instanceof ConfigEntityInterface;
  }

  // Don't block particular patterns.
  if ($mark_form_read_only && $form_object instanceof EntityFormInterface) {
    $entity = $form_object
      ->getEntity();
    $name = $entity
      ->getConfigDependencyName();
    if ($this
      ->matchesWhitelistPattern($name)) {
      $mark_form_read_only = FALSE;
    }
  }
  if ($mark_form_read_only && $form_object instanceof ConfigFormBase) {

    // Get the editable configuration names.
    $editable_config = $this
      ->getEditableConfigNames($form_object);

    // If all editable config is in the whitelist, do not block the form.
    if ($editable_config == array_filter($editable_config, [
      $this,
      'matchesWhitelistPattern',
    ])) {
      $mark_form_read_only = FALSE;
    }
  }
  if ($mark_form_read_only) {
    $event
      ->markFormReadOnly();
  }
}