|
2 | 2 |
|
3 | 3 | use Drupal\taxonomy\Entity\Term; |
4 | 4 | use Drupal\taxonomy\Entity\Vocabulary; |
| 5 | +use Drupal\field\Entity\FieldStorageConfig; |
| 6 | +use Drupal\field\Entity\FieldConfig; |
| 7 | +use Drupal\Core\Entity\Entity\EntityFormDisplay; |
| 8 | +use Drupal\Core\Entity\Entity\EntityViewDisplay; |
5 | 9 |
|
6 | 10 | /** |
7 | 11 | * Implements hook_install(). |
8 | 12 | */ |
9 | 13 | function ubc_cwl_auth_install() { |
10 | 14 | ubc_cwl_auth_createCwlRole(); |
11 | 15 | ubc_cwl_auth_createVisibilityTaxonomy(); |
| 16 | + ubc_cwl_auth_createVisibilityField(); |
12 | 17 | } |
13 | 18 |
|
14 | 19 | function ubc_cwl_auth_createCwlRole() { |
@@ -92,3 +97,67 @@ function ubc_cwl_auth_createVisibilityTaxonomy() { |
92 | 97 |
|
93 | 98 | } |
94 | 99 |
|
| 100 | +function ubc_cwl_auth_createVisibilityField() { |
| 101 | + $entity_type = 'node'; |
| 102 | + $bundle = 'page'; |
| 103 | + $field_name = 'field_visibility'; |
| 104 | + $vocabulary = 'visibility'; |
| 105 | + |
| 106 | + if (!FieldStorageConfig::loadByName($entity_type, $field_name)) { |
| 107 | + FieldStorageConfig::create([ |
| 108 | + 'field_name' => $field_name, |
| 109 | + 'entity_type' => $entity_type, |
| 110 | + 'type' => 'entity_reference', |
| 111 | + 'cardinality' => 1, |
| 112 | + 'settings' => [ |
| 113 | + 'target_type' => 'taxonomy_term', |
| 114 | + ], |
| 115 | + 'translatable' => TRUE, |
| 116 | + ])->save(); |
| 117 | + \Drupal::logger('field_setup')->info('Created field storage for @field.', ['@field' => $field_name]); |
| 118 | + } |
| 119 | + |
| 120 | + if (!FieldConfig::loadByName($entity_type, $bundle, $field_name)) { |
| 121 | + FieldConfig::create([ |
| 122 | + 'entity_type' => $entity_type, |
| 123 | + 'bundle' => $bundle, |
| 124 | + 'field_name' => $field_name, |
| 125 | + 'label' => 'Visibility', |
| 126 | + 'description' => 'Select visibility level for this page.', |
| 127 | + 'required' => TRUE, |
| 128 | + 'settings' => [ |
| 129 | + 'handler' => 'default:taxonomy_term', |
| 130 | + 'handler_settings' => [ |
| 131 | + 'target_bundles' => [ |
| 132 | + $vocabulary => $vocabulary, |
| 133 | + ], |
| 134 | + 'auto_create' => FALSE, |
| 135 | + ], |
| 136 | + ], |
| 137 | + 'required' => FALSE, |
| 138 | + ])->save(); |
| 139 | + \Drupal::logger('field_setup')->info('Created field instance for @field on @bundle.', ['@field' => $field_name, '@bundle' => $bundle]); |
| 140 | + } |
| 141 | + |
| 142 | + $form_display = \Drupal::entityTypeManager()->getStorage('entity_form_display')->load("{$entity_type}.{$bundle}.default"); |
| 143 | + if ($form_display) { |
| 144 | + $form_display->setComponent($field_name, [ |
| 145 | + 'type' => 'options_select', |
| 146 | + 'weight' => 122, |
| 147 | + 'region' => 'content', |
| 148 | + ])->save(); |
| 149 | + } |
| 150 | + |
| 151 | + $view_display = \Drupal::entityTypeManager()->getStorage('entity_view_display')->load("{$entity_type}.{$bundle}.default"); |
| 152 | + if ($view_display) { |
| 153 | + $view_display->setComponent($field_name, [ |
| 154 | + 'type' => 'entity_reference_label', |
| 155 | + 'label' => 'above', |
| 156 | + ])->save(); |
| 157 | + } |
| 158 | + |
| 159 | + \Drupal::logger('field_setup')->info('Field @field successfully created for content type @bundle.', [ |
| 160 | + '@field' => $field_name, |
| 161 | + '@bundle' => $bundle, |
| 162 | + ]); |
| 163 | +} |
0 commit comments