You are here

function tca_form_alter in Token Content Access 2.0.x

Same name and namespace in other branches
  1. 8 tca.module \tca_form_alter()

Implements hook_form_alter().

File

./tca.module, line 36
Contains tca.module.

Code

function tca_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  $tca_plugin = \Drupal::service('plugin.manager.tca_plugin');
  $affected_types = $tca_plugin
    ->loadSupportedEntityTypes();
  $affected_bundle_types = $tca_plugin
    ->loadSupportedBundleEntityTypes();
  if ($form_state
    ->getFormObject() instanceof EntityFormInterface) {
    $current_type = $form_state
      ->getFormObject()
      ->getEntity()
      ->getEntityTypeId();
    $current_operation = $form_state
      ->getFormObject()
      ->getOperation();
    $administer_permitted = \Drupal::currentUser()
      ->hasPermission('tca administer ' . $current_type);

    // Skip delete and cencel forms.
    if (!in_array($current_operation, [
      'delete',
      'cancel',
    ])) {
      if (in_array($current_type, $affected_types) && $administer_permitted || in_array($current_type, $affected_bundle_types) || $current_operation === 'add' && _tca_is_forced($form_state
        ->getFormObject()
        ->getEntity())) {
        \Drupal::service('tca.form_mangler')
          ->addTcaSettingsToEntityForm($form, $form_state
          ->getFormObject()
          ->getEntity(), $form_state, $form_id);
        $form['#attached']['library'][] = 'tca/form';
      }
    }
  }
}