The foundation of Drupal's data model are entities, which offer an organized and adaptable method of managing various kinds of data and content on a website. With an emphasis on nodes, taxonomy words, users, files, and custom entities, this article will examine Drupal entities and provide code examples to help you better understand how to create, manage, and use them.
Nodes are entities in Drupal that stand in for content items like pages, articles, and blog entries. The fields and behavior of each node are defined by the content type to which it belongs.
use Drupal\node\Entity\Node;
// Create a new node of type 'article'
$node = Node::create([
'type' => 'article', // Content type machine name
'title' => 'My First Article',
'body' => [
'value' => 'This is the body text of the article.',
'format' => 'basic_html',
],
'status' => 1, // Publish status: 1 for published, 0 for unpublished
'uid' => 1, // User ID of the author
]);
// Save the node
$node->save();
Explanation: In this example:
$node->save()
.In a Drupal site, taxonomy terms are entities used to classify and organize content. Vocabularies, or categories of terms, are used to arrange terms.
use Drupal\taxonomy\Entity\Term;
// Create a new taxonomy term in the 'Tags' vocabulary
$term = Term::create([
'vid' => 'tags', // Vocabulary machine name
'name' => 'Drupal', // Term name
]);
// Save the term
$term->save();
Explanation: This code snippet:
vid
to "tags" to define the vocabulary.In Drupal, users are entities that represent contributors or site members. They can be created or altered programmatically and can have unique roles and fields.
use Drupal\user\Entity\User;
// Create a new user
$user = User::create([
'name' => 'jdoe',
'mail' => '[email protected]',
'pass' => user_password(),
'status' => 1, // 1 for active, 0 for blocked
'roles' => ['editor'], // Assign 'editor' role
]);
// Save the user
$user->save();
Explanation: In this example:
Images, documents, and videos are examples of media assets that can be reused throughout content and are represented by files in Drupal.
use Drupal\file\Entity\File;
// Load a file from a local path
$file_path = 'public://example_image.jpg';
$file = File::create([
'uri' => $file_path,
'status' => 1,
]);
// Save the file entity
$file->save();
Developers can construct specific data structures using custom entities when none of the normal entity types are suitable. Custom modules are frequently used to create custom entities, which are defined with unique fields, actions, and permissions.
namespace Drupal\my_module\Entity;
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
/**
* Defines the Product entity.
*
* @ContentEntityType(
* id = "product",
* label = @Translation("Product"),
* base_table = "products",
* entity_keys = {
* "id" = "id",
* "label" = "name",
* },
* )
*/
class Product extends ContentEntityBase {
// Define base fields for the custom entity
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields['name'] = BaseFieldDefinition::create('string')
->setLabel(t('Name'))
->setRequired(TRUE);
$fields['price'] = BaseFieldDefinition::create('decimal')
.setLabel(t('Price'))
.setSettings([
'precision' => 10,
'scale' => 2,
]);
return $fields;
}
}
Using Drupal's Entity API, you may programmatically load and query entities. Nodes of type "article" can be queried as follows:
use Drupal\node\Entity\Node;
// Load multiple nodes of type 'article'
$nids = \Drupal::entityQuery('node')
->condition('type', 'article')
->condition('status', 1) // Only published nodes
->execute();
$nodes = Node::loadMultiple($nids);
Explanation: This code:
entityQuery
to fetch node IDs for published nodes of type "article".Node::loadMultiple()
.Drupal entities offer a powerful method for organizing and managing data. Drupal's Entity API is flexible and strong, whether you're building nodes, classifying content using taxonomy terms, processing files, managing user accounts, or creating custom entities. You can increase the functionality of your Drupal site and improve the organization of your content by learning about the distinctive features of each entity type and exploring code examples.
Published By: Meghna Batra
Updated at: 2024-10-28 16:43:10