Commit 5863929d authored by catch's avatar catch
Browse files

Issue #3408698 by lauriii, alexpott: Provide alter for the field type UI definitions

parent 7af03479
Loading
Loading
Loading
Loading
Loading
+4 −12
Original line number Diff line number Diff line
@@ -238,23 +238,15 @@ function comment_form_field_ui_field_storage_add_form_alter(&$form, FormStateInt
  if ($form_state->get('entity_type_id') == 'comment' && $route_match->getParameter('commented_entity_type')) {
    $form['#title'] = \Drupal::service('comment.manager')->getFieldUIPageTitle($route_match->getParameter('commented_entity_type'), $route_match->getParameter('field_name'));
  }
  if (!_comment_entity_uses_integer_id($form_state->get('entity_type_id'))) {
    $form['add']['new_storage_type']['#process'][] = 'comment_new_storage_type_process_callback';
  }
}

/**
 * Process callback to remove comment type field option.
 * Implements hook_field_info_entity_type_ui_definitions_alter().
 */
function comment_new_storage_type_process_callback($element, &$form_state, $form) {
  foreach ($element as $key => $value) {
    if (isset($value['radio']['#return_value']) && $value['radio']['#return_value'] === 'comment') {
      // You cannot use comment fields on entity types with non-integer IDs.
      unset($element[$key]);
    }
function comment_field_info_entity_type_ui_definitions_alter(array &$ui_definitions, string $entity_type_id) {
  if (!_comment_entity_uses_integer_id($entity_type_id)) {
    unset($ui_definitions['comment']);
  }

  return $element;
}

/**
+18 −0
Original line number Diff line number Diff line
@@ -57,6 +57,24 @@ function hook_field_info_alter(&$info) {
  }
}

/**
 * Alters the UI field definitions.
 *
 * This hook can be used for altering field definitions available in the UI
 * dynamically per entity type. For example, it can be used to hide field types
 * that are incompatible with an entity type.
 *
 * @param array $ui_definitions
 *   Definition of all field types that can be added via UI.
 * @param string $entity_type_id
 *   The entity type id.
 */
function hook_field_info_entity_type_ui_definitions_alter(array &$ui_definitions, string $entity_type_id) {
  if ($entity_type_id === 'node') {
    unset($ui_definitions['field_type_not_compatible_with_node']);
  }
}

/**
 * Perform alterations on preconfigured field options.
 *
+10 −0
Original line number Diff line number Diff line
@@ -15,6 +15,7 @@
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\field\FieldStorageConfigInterface;

require_once __DIR__ . '/field_test.entity.inc';
@@ -215,3 +216,12 @@ function field_test_field_ui_preconfigured_options_alter(array &$options, $field
    ];
  }
}

/**
 * Implements hook_field_info_entity_type_ui_definitions_alter().
 */
function field_test_field_info_entity_type_ui_definitions_alter(array &$ui_definitions, string $entity_type_id) {
  if ($entity_type_id === 'node') {
    $ui_definitions['boolean']['label'] = new TranslatableMarkup('Boolean (overridden by alter)');
  }
}
+3 −1
Original line number Diff line number Diff line
@@ -150,7 +150,9 @@ public function buildForm(array $form, FormStateInterface $form_state, $entity_t
    ];

    $field_type_options = $unique_definitions = [];
    $grouped_definitions = $this->fieldTypePluginManager->getGroupedDefinitions($this->fieldTypePluginManager->getUiDefinitions(), 'label', 'id');
    $ui_definitions = $this->fieldTypePluginManager->getUiDefinitions();
    \Drupal::moduleHandler()->alter('field_info_entity_type_ui_definitions', $ui_definitions, $this->entityTypeId);
    $grouped_definitions = $this->fieldTypePluginManager->getGroupedDefinitions($ui_definitions, 'label', 'id');
    $category_definitions = $this->fieldTypeCategoryManager->getDefinitions();
    // Invoke a hook to get category properties.
    foreach ($grouped_definitions as $category => $field_types) {
+11 −0
Original line number Diff line number Diff line
@@ -345,4 +345,15 @@ public function testFieldTypeCardinalityAlter() {
    $this->assertSession()->elementTextContains('css', '#edit-field-storage', 'Greetings from Drupal\field_test\Plugin\Field\FieldType\TestItem::storageSettingsForm');
  }

  /**
   * Tests hook_field_info_entity_type_ui_definitions_alter().
   */
  public function testFieldUiDefinitionsAlter() {
    $user = $this->drupalCreateUser(['administer node fields']);
    $node_type = $this->drupalCreateContentType();
    $this->drupalLogin($user);
    $this->drupalGet('/admin/structure/types/manage/' . $node_type->id() . '/fields/add-field');
    $this->assertSession()->pageTextContains('Boolean (overridden by alter)');
  }

}