$npx -y skills add edutrul/drupal-ai --skill drupal-entity-apiDrupal Entity API — loading, creating, updating, and deleting entities using entity_type.manager. Node, User, Term, Media patterns.
| 1 | # Drupal Entity API |
| 2 | |
| 3 | ## Loading Entities |
| 4 | |
| 5 | ```php |
| 6 | // Load a single node |
| 7 | $node = $this->entityTypeManager->getStorage('node')->load($nid); |
| 8 | |
| 9 | // Load multiple nodes |
| 10 | $nodes = $this->entityTypeManager->getStorage('node')->loadMultiple([1, 2, 3]); |
| 11 | |
| 12 | // Load by properties |
| 13 | $nodes = $this->entityTypeManager->getStorage('node')->loadByProperties([ |
| 14 | 'type' => 'article', |
| 15 | 'status' => 1, |
| 16 | 'uid' => $uid, |
| 17 | ]); |
| 18 | |
| 19 | // Load user |
| 20 | $user = $this->entityTypeManager->getStorage('user')->load($uid); |
| 21 | |
| 22 | // Load term |
| 23 | $term = $this->entityTypeManager->getStorage('taxonomy_term')->load($tid); |
| 24 | |
| 25 | // Load media |
| 26 | $media = $this->entityTypeManager->getStorage('media')->load($mid); |
| 27 | ``` |
| 28 | |
| 29 | ## Entity Query |
| 30 | |
| 31 | ```php |
| 32 | $query = $this->entityTypeManager->getStorage('node')->getQuery() |
| 33 | ->accessCheck(TRUE) |
| 34 | ->condition('type', 'article') |
| 35 | ->condition('status', 1) |
| 36 | ->condition('field_tags', $tid) |
| 37 | ->sort('created', 'DESC') |
| 38 | ->range(0, 10); |
| 39 | |
| 40 | $nids = $query->execute(); |
| 41 | $nodes = $this->entityTypeManager->getStorage('node')->loadMultiple($nids); |
| 42 | ``` |
| 43 | |
| 44 | ## Creating Entities |
| 45 | |
| 46 | ```php |
| 47 | // Create a node |
| 48 | $node = $this->entityTypeManager->getStorage('node')->create([ |
| 49 | 'type' => 'article', |
| 50 | 'title' => 'My Article', |
| 51 | 'status' => 1, |
| 52 | 'uid' => $this->currentUser->id(), |
| 53 | 'field_body' => [ |
| 54 | 'value' => 'Content here', |
| 55 | 'format' => 'basic_html', |
| 56 | ], |
| 57 | ]); |
| 58 | $node->save(); |
| 59 | |
| 60 | // Create a taxonomy term |
| 61 | $term = $this->entityTypeManager->getStorage('taxonomy_term')->create([ |
| 62 | 'vid' => 'tags', |
| 63 | 'name' => 'New Tag', |
| 64 | ]); |
| 65 | $term->save(); |
| 66 | ``` |
| 67 | |
| 68 | ## Updating Entities |
| 69 | |
| 70 | ```php |
| 71 | $node = $this->entityTypeManager->getStorage('node')->load($nid); |
| 72 | if ($node instanceof NodeInterface) { |
| 73 | $node->set('title', 'Updated Title'); |
| 74 | $node->set('field_body', ['value' => 'New content', 'format' => 'basic_html']); |
| 75 | $node->save(); |
| 76 | } |
| 77 | ``` |
| 78 | |
| 79 | ## Deleting Entities |
| 80 | |
| 81 | ```php |
| 82 | $node = $this->entityTypeManager->getStorage('node')->load($nid); |
| 83 | if ($node) { |
| 84 | $node->delete(); |
| 85 | } |
| 86 | |
| 87 | // Delete multiple |
| 88 | $storage = $this->entityTypeManager->getStorage('node'); |
| 89 | $nodes = $storage->loadMultiple($nids); |
| 90 | $storage->delete($nodes); |
| 91 | ``` |
| 92 | |
| 93 | ## Accessing Field Values |
| 94 | |
| 95 | ```php |
| 96 | // Simple field |
| 97 | $title = $node->get('title')->value; |
| 98 | $status = $node->get('status')->value; |
| 99 | |
| 100 | // Text with format |
| 101 | $body = $node->get('body')->value; |
| 102 | $format = $node->get('body')->format; |
| 103 | |
| 104 | // Reference field |
| 105 | $tid = $node->get('field_tags')->target_id; |
| 106 | $term = $node->get('field_tags')->entity; |
| 107 | |
| 108 | // Multi-value field |
| 109 | foreach ($node->get('field_images') as $item) { |
| 110 | $fid = $item->target_id; |
| 111 | $file = $item->entity; |
| 112 | } |
| 113 | |
| 114 | // Check if field is empty |
| 115 | if (!$node->get('field_image')->isEmpty()) { ... } |
| 116 | ``` |
| 117 | |
| 118 | ## Access Checking |
| 119 | |
| 120 | ```php |
| 121 | // Always add access checks |
| 122 | $query->accessCheck(TRUE); |
| 123 | |
| 124 | // Check entity access |
| 125 | if ($node->access('view', $account)) { ... } |
| 126 | if ($node->access('update', $account)) { ... } |
| 127 | ``` |
| 128 | |
| 129 | ## Inject EntityTypeManager |
| 130 | |
| 131 | ```php |
| 132 | public function __construct( |
| 133 | private readonly EntityTypeManagerInterface $entityTypeManager, |
| 134 | ) {} |
| 135 | ``` |