You are here

function rabbit_hole_form_alter in Rabbit Hole 2.x

Same name and namespace in other branches
  1. 8 rabbit_hole.module \rabbit_hole_form_alter()
  2. 6 rabbit_hole.module \rabbit_hole_form_alter()

Implements hook_form_alter().

File

./rabbit_hole.module, line 44
Contains rabbit_hole.module.

Code

function rabbit_hole_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  $affected_types = \Drupal::service('plugin.manager.rabbit_hole_entity_plugin')
    ->loadSupportedEntityTypes();
  $affected_bundle_types = \Drupal::service('plugin.manager.rabbit_hole_entity_plugin')
    ->loadSupportedBundleEntityTypes();
  $affected_global_forms = \Drupal::service('plugin.manager.rabbit_hole_entity_plugin')
    ->loadSupportedGlobalForms();
  if ($form_state
    ->getFormObject() instanceof EntityFormInterface) {
    $current_type = $form_state
      ->getFormObject()
      ->getEntity()
      ->getEntityTypeId();
    $current_operation = $form_state
      ->getFormObject()
      ->getOperation();
    $disallowed_operations = [
      'delete',
      'cancel',
      'reset',
      'layout_builder',
      'replicate',
    ];
    $administer_permitted = \Drupal::currentUser()
      ->hasPermission('rabbit hole administer ' . $current_type);
    $is_entity_form = in_array($current_type, $affected_types) && $administer_permitted && !in_array($current_operation, $disallowed_operations);
    $is_bundle_form = in_array($current_type, $affected_bundle_types) && !in_array($current_operation, $disallowed_operations);
    if ($is_entity_form || $is_bundle_form) {
      \Drupal::service('rabbit_hole.form_mangler')
        ->addRabbitHoleOptionsToEntityForm($form, $form_state
        ->getFormObject()
        ->getEntity(), $form_state, $form_id);
    }
  }
  elseif (array_key_exists($form_id, $affected_global_forms)) {
    \Drupal::service('rabbit_hole.form_mangler')
      ->addRabbitHoleOptionsToGlobalForm($form, $affected_global_forms[$form_id], $form_state, $form_id);
  }
}