$npx -y skills add edutrul/drupal-ai --skill drupal-fieldsDrupal fields — field types, widgets, formatters, drush field:create, field storage/config, and accessing single-value and multi-value field values in PHP code.
| 1 | # Drupal Fields |
| 2 | |
| 3 | ## Creating Fields (Drush — CLI First) |
| 4 | |
| 5 | **Always use `drush field:create` instead of manually creating field config files.** |
| 6 | |
| 7 | ```bash |
| 8 | # Interactive (recommended first time) |
| 9 | drush field:create |
| 10 | |
| 11 | # Non-interactive |
| 12 | drush field:create node article \ |
| 13 | --field-name=field_subtitle \ |
| 14 | --field-label="Subtitle" \ |
| 15 | --field-type=string \ |
| 16 | --field-widget=string_textfield \ |
| 17 | --is-required=0 \ |
| 18 | --cardinality=1 |
| 19 | |
| 20 | # Reference field |
| 21 | drush field:create node article \ |
| 22 | --field-name=field_tags \ |
| 23 | --field-label="Tags" \ |
| 24 | --field-type=entity_reference \ |
| 25 | --field-widget=entity_reference_autocomplete \ |
| 26 | --cardinality=-1 \ |
| 27 | --target-type=taxonomy_term |
| 28 | |
| 29 | # Image field |
| 30 | drush field:create node article \ |
| 31 | --field-name=field_image \ |
| 32 | --field-label="Image" \ |
| 33 | --field-type=image \ |
| 34 | --field-widget=image_image \ |
| 35 | --is-required=0 \ |
| 36 | --cardinality=1 |
| 37 | ``` |
| 38 | |
| 39 | ## Managing Fields |
| 40 | |
| 41 | ```bash |
| 42 | # List all fields on a content type |
| 43 | drush field:info node article |
| 44 | |
| 45 | # Delete a field |
| 46 | drush field:delete node.article.field_subtitle |
| 47 | |
| 48 | # Discover available types/widgets/formatters |
| 49 | drush field:types |
| 50 | drush field:widgets |
| 51 | drush field:formatters |
| 52 | ``` |
| 53 | |
| 54 | ## Export After Creating Fields |
| 55 | |
| 56 | ```bash |
| 57 | # Always export config after CLI changes |
| 58 | drush config:export -y |
| 59 | ``` |
| 60 | |
| 61 | ## Custom Field Type (Plugin) |
| 62 | |
| 63 | ```bash |
| 64 | drush generate plugin:field:type |
| 65 | drush generate plugin:field:formatter |
| 66 | drush generate plugin:field:widget |
| 67 | ``` |
| 68 | |
| 69 | ## Accessing Field Values in Code |
| 70 | |
| 71 | ```php |
| 72 | // Single value |
| 73 | $value = $entity->get('field_name')->value; |
| 74 | |
| 75 | // Entity reference |
| 76 | $referenced = $entity->get('field_ref')->entity; |
| 77 | |
| 78 | // Multi-value |
| 79 | foreach ($entity->get('field_multi') as $item) { |
| 80 | $value = $item->value; |
| 81 | } |
| 82 | ``` |