You are here

function disable_field_form_alter in Disable Field 8

Same name and namespace in other branches
  1. 7 disable_field.module \disable_field_form_alter()

Implements hook_form_alter().

File

./disable_field.module, line 132

Code

function disable_field_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  list($form_entity_type_id, $bundle, $operation) = disable_field_get_entity_data($form_state);

  // Disable Field.
  if (!empty($form_entity_type_id) && !empty($bundle)) {
    $entity_type = \Drupal::service('entity_type.manager')
      ->getDefinition($form_entity_type_id);

    // Check that current entity is fieldable before process it's fields.
    if ($entity_type
      ->isSubclassOf(\Drupal\Core\Entity\FieldableEntityInterface::class)) {
      $fields = \Drupal::service('entity_field.manager')
        ->getFieldDefinitions($form_entity_type_id, $bundle);
      $user_roles = \Drupal::currentUser()
        ->getRoles();
      foreach ($fields as $field_name => $field_definition) {
        if (method_exists($field_definition, 'getThirdPartySettings')) {
          if ($settings = $field_definition
            ->getThirdPartySettings('disable_field')) {
            if ($field_definition
              ->getType() == 'field_collection') {
              $bundle_fields = \Drupal::entityManager()
                ->getFieldDefinitions('field_collection_item', $field_name);
              foreach ($bundle_fields as $field_collection_name => $field_collection_definition) {
                if (strpos($field_collection_name, 'field_') !== FALSE && $field_collection_name != 'field_name') {
                  $field_collection_settings = $field_collection_definition
                    ->getThirdPartySettings('disable_field');
                  if (!empty($field_collection_settings["{$operation}_disable"])) {

                    // Field is disabled for all roles.
                    if ($field_collection_settings["{$operation}_disable"] == 'all') {
                      $form[$field_name]['widget'][0][$field_collection_name]['#disabled'] = TRUE;
                    }
                    else {
                      if ($field_collection_settings["{$operation}_disable"] == 'roles') {

                        // @todo seems it's not the best way to check permission. What if user has several roles?
                        $disable_check = array_diff($field_collection_settings["{$operation}_disable_roles"], $user_roles);
                        if (count($disable_check) < count($field_collection_settings["{$operation}_disable_roles"])) {
                          $form[$field_name]['widget'][0][$field_collection_name]['#disabled'] = TRUE;
                        }
                      }
                    }
                  }
                }
              }
            }

            // If field is disabled on the page with current type.
            if (!empty($settings["{$operation}_disable"])) {

              // Field is disabled for all roles.
              if ($settings["{$operation}_disable"] == 'all') {
                $form[$field_name]['#disabled'] = TRUE;
                disable_field_add_validator($form, $field_name);
              }
              else {
                if ($settings["{$operation}_disable"] == 'roles') {

                  // @todo seems it's not the best way to check permission. What if user has several roles?
                  $disable_check = array_diff($settings["{$operation}_disable_roles"], $user_roles);
                  if (count($disable_check) < count($settings["{$operation}_disable_roles"])) {
                    $form[$field_name]['#disabled'] = TRUE;
                    disable_field_add_validator($form, $field_name);
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}