Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
27 / 27 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| FieldHooks | |
100.00% |
27 / 27 |
|
100.00% |
2 / 2 |
5 | |
100.00% |
1 / 1 |
| fieldViewsData | |
100.00% |
26 / 26 |
|
100.00% |
1 / 1 |
4 | |||
| fieldTypeCategoryInfoAlter | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Drupal\name\Hook; |
| 6 | |
| 7 | use Drupal\Component\Utility\DeprecationHelper; |
| 8 | use Drupal\Core\Field\FieldTypeCategoryManagerInterface; |
| 9 | use Drupal\Core\Hook\Attribute\Hook; |
| 10 | use Drupal\field\FieldStorageConfigInterface; |
| 11 | |
| 12 | /** |
| 13 | * Hook implementations for field API integration (Views, categories). |
| 14 | * |
| 15 | * @internal |
| 16 | */ |
| 17 | final class FieldHooks { |
| 18 | |
| 19 | /** |
| 20 | * Implements hook_field_views_data(). |
| 21 | */ |
| 22 | // phpcs:ignore Drupal.Commenting.PostStatementComment.Found -- #[Hook] is a PHP attribute, not a trailing comment. |
| 23 | #[Hook('field_views_data')] // @phpstan-ignore attribute.notFound |
| 24 | public function fieldViewsData(FieldStorageConfigInterface $field_storage): array { |
| 25 | // `views.field_data_provider` exists on Drupal 11.2+. Fall back to the |
| 26 | // procedural helper on earlier versions. Use the global container so we |
| 27 | // do not require the missing service to be resolvable via autowire. |
| 28 | $data = DeprecationHelper::backwardsCompatibleCall( |
| 29 | currentVersion: \Drupal::VERSION, |
| 30 | deprecatedVersion: '11.2.0', |
| 31 | // @phpstan-ignore-next-line globalDrupalDependencyInjection.useDependencyInjection |
| 32 | currentCallable: fn() => \Drupal::service('views.field_data_provider') |
| 33 | ->defaultFieldImplementation($field_storage), |
| 34 | deprecatedCallable: fn() => views_field_default_views_data($field_storage), |
| 35 | ); |
| 36 | |
| 37 | $field_name = $field_storage->getName(); |
| 38 | $field_type = $field_storage->getType(); |
| 39 | |
| 40 | $columns = [ |
| 41 | 'title' => 'standard', |
| 42 | 'given' => 'standard', |
| 43 | 'middle' => 'standard', |
| 44 | 'family' => 'standard', |
| 45 | 'generational' => 'standard', |
| 46 | 'credentials' => 'standard', |
| 47 | ]; |
| 48 | |
| 49 | $table_names = array_keys($data); |
| 50 | foreach ($table_names as $table_name) { |
| 51 | $data[$table_name][$field_name]['filter'] = [ |
| 52 | 'field' => $field_name, |
| 53 | 'table' => $table_name, |
| 54 | 'field_name' => $field_name, |
| 55 | 'id' => 'name_fulltext', |
| 56 | 'allow_empty' => TRUE, |
| 57 | ]; |
| 58 | |
| 59 | if ($field_type === 'name') { |
| 60 | // Add every name column as a view field for every name field. |
| 61 | foreach ($columns as $column => $plugin_id) { |
| 62 | $data[$table_name][$field_name . '_' . $column]['field'] = [ |
| 63 | 'id' => $plugin_id, |
| 64 | 'field_name' => $field_name, |
| 65 | 'property' => $column, |
| 66 | ]; |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | return $data; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Implements hook_field_type_category_info_alter(). |
| 76 | */ |
| 77 | // phpcs:ignore Drupal.Commenting.PostStatementComment.Found -- #[Hook] is a PHP attribute, not a trailing comment. |
| 78 | #[Hook('field_type_category_info_alter')] // @phpstan-ignore attribute.notFound |
| 79 | public function fieldTypeCategoryInfoAlter(array &$definitions): void { |
| 80 | // The `name` field type belongs in the `general` category, so the |
| 81 | // libraries need to be attached using an alter hook. |
| 82 | $definitions[FieldTypeCategoryManagerInterface::FALLBACK_CATEGORY]['libraries'][] = 'name/field_ui'; |
| 83 | } |
| 84 | |
| 85 | } |