9.11.1. प्रोग्राम के माध्यम से एंटिटी फ़ील्ड्स के साथ काम करना
Drupal में एंटिटी के भीतर फ़ील्ड मान प्राप्त करना काफी सरल है, लेकिन ऐसा करने के कई तरीके हैं। आइए देखें कि कस्टम कोड में फ़ील्ड वैल्यूज़ के साथ काम करने का सबसे अच्छा तरीका क्या है। आप हमेशा Drupal की आधिकारिक वेबसाइट पर फ़ील्ड्स के साथ काम करने की नवीनतम जानकारी देख सकते हैं:
https://www.drupal.org/docs/8/api/entity-api/working-with-the-entity-api
इस लेख में, हम मानों (values) के साथ काम करने के उदाहरणों को देखेंगे।
आपको यह याद रखने की ज़रूरत नहीं है कि कोई विशेष फ़ील्ड कैसे काम करती है — आप हमेशा इस पेज पर वापस आ सकते हैं और देख सकते हैं। समय के साथ, आप Drupal के दस्तावेज़ों की ओर अधिक बार लौटेंगे और महसूस करेंगे कि Drupal में फ़ील्ड्स के साथ काम करना कितना आसान है। तब तक, आप इस पेज को बुकमार्क कर सकते हैं ताकि यह एक cheat sheet के रूप में हमेशा आपके पास रहे।
नोड्स (Nodes) के साथ काम करना
nid द्वारा नोड लोड करना:
$nid = 234;
$node_storage = \Drupal::entityTypeManager()->getStorage('node');
$node = $node_storage->load($nid);
नोड का ID प्राप्त करना:
$node->id();
नोड/एंटिटी का बंडल प्राप्त करना:
$node->bundle();
$entity->getType();
फ़ील्ड वैल्यूज़ प्राप्त करना:
$node->get('title')->value;
$node->get('created')->value;
$node->get('body')->value;
$node->get('body')->summary;
$node->get('field_foo')->value;
$node->get('field_image')->target_id;
वैल्यूज़ प्राप्त करने के लिए एक छोटा सिंटैक्स भी इस्तेमाल किया जा सकता है:
$node->title->value;
$node->created->value;
$node->body->value;
$node->body->summary;
$node->field_foo->value;
$node->field_image->target_id;
फ़ील्ड वैल्यू के आधार पर विशेष नोड्स लोड करना:
$query = \Drupal::entityQuery('node')
->condition('type', 'article')
->condition('field_terms', 42);
$nids = $query->execute();
$nodes = $node_storage->loadMultiple($nids);
foreach ($nodes as $node) {
print $node->title->value;
$node->set('title', "New title for node");
$node->save();
}
फ़ील्ड्स में मान बदलना:
$node->set('title', "New title");
$node->set('body', array(
'summary' => "Teaser",
'value' => "Long text",
'format' => 'basic_html',
));
$node->save();
सिंगल-वैल्यू फ़ील्ड्स के लिए शॉर्ट सिंटैक्स:
$node->title = 'New title';
$node->field_text = 'text';
एकाधिक फ़ील्ड वैल्यूज़ प्राप्त करना:
$nids = \Drupal::entityQuery('node')->condition('type', 'album')->execute();
$nodes = Node::loadMultiple($nids);
$data = array();
foreach($nodes as $node) {
$photo = array();
foreach($node->get('field_image')->getValue() as $file){
$fid = $file['target_id']; // फ़ाइल का fid प्राप्त करें;
$photo[] = \Drupal\file\Entity\File::load($fid)->getFileUri();
}
$data[] = array(
'album_name' => $node->get('field_album_name')->getValue(),
'place' => $node->get('field_place')->getValue(),
'author' => $node->get('field_author')->getValue(),
'photo' => $photo,
);
}
फ़ाइल फ़ील्ड्स के साथ काम करना
फ़ाइलें अन्य एंटिटीज़ में रेफरेंस फ़ील्ड्स के माध्यम से जोड़ी जाती हैं। जब हम इन फ़ील्ड्स तक पहुँचते हैं, तो हम फ़ाइल का ID प्राप्त कर सकते हैं और फिर उस ID से फ़ाइल की जानकारी प्राप्त कर सकते हैं।
फ़ाइल को ID से प्राप्त करना:
$fid = 42;
$file_storage = \Drupal::entityTypeManager()->getStorage('file');
$file = $file_storage->load($fid);
नोड फ़ील्ड से फ़ाइल ऑब्जेक्ट प्राप्त करना:
$file = $node->field_image->entity;
फ़ाइल ऑब्जेक्ट के कुछ फ़ील्ड प्राप्त करना:
$file->getFileUri(); // "public://file123.jpg"
// URI से URL में बदलना: file_url_transform_relative(file_create_url($file->getFileUri()));
// "/sites/default/files/public/file123.jpg"
$file->filename->value; // "file123.jpg"
$file->filemime->value; // "image/jpeg"
$file->filesize->value; // 63518 (बाइट्स में आकार)
$file->created->value; // 1511206249 (Unix timestamp)
$file->changed->value; // 1511234256 (Unix timestamp)
$file->id();
फ़ाइल की प्रॉपर्टीज़ देखना (जो file_managed
टेबल में संग्रहीत हैं):
echo $file->uid->target_id; // 1
echo $file->uid->value; // काम नहीं करेगा! target_id उपयोग करें।
echo $file->uid->entity->name->value;
echo $file->uid->entity->timezone->value; // "Asia/Omsk"
Entity Reference फ़ील्ड्स के साथ काम करना
आप रेफरेंस फ़ील्ड्स से कई मान प्राप्त कर सकते हैं और foreach के माध्यम से उन्हें प्रोसेस कर सकते हैं:
foreach ($node->field_my_entity_reference as $reference) {
print $reference->target_id;
print $reference->entity->title->value;
}
Entity Reference मल्टीपल फ़ील्ड को संशोधित करना:
$nids = [3,4,5,6];
$node->set('field_my_entity_reference', $nids);
$node->save();
मौजूदा मानों में नए रेफरेंस मान जोड़ना:
$nids = [3,4,5,6]; // उदाहरण मान
foreach ($nids as $nid) {
$node->field_my_entity_reference[] = [
'target_id' => $nid
];
}
$node->save();
फ़ील्ड्स में पुराने और नए मानों की तुलना करना
FieldItemListInterface
(जो प्रत्येक फ़ील्ड के पीछे की क्लास है) में equals()
नामक एक मेथड होता है:
/**
* Implements hook_entity_update().
*/
function mymodule_entity_update(\Drupal\Core\Entity\EntityInterface $entity) {
// सुनिश्चित करें कि एंटिटी के पास यह फ़ील्ड है।
if (!$entity->hasField('field_wikipage_access') || !isset($entity->original)) {
return;
}
$new_values = $entity->get('field_wikipage_access');
$old_values = $entity->original->get('field_wikipage_access');
// बूलियन जाँच: बदला या नहीं?
if ($new_values->equals($old_values)) {
return; // कुछ नहीं बदला, जल्दी लौटें।
}
// …अन्यथा आगे जारी रखें (नीचे देखें)।
}
पैराग्राफ्स (Paragraphs) के साथ काम करना
$my_paragraph = null;
foreach ($node->get('field_paragraph_reference') as $paragraph) {
if ($paragraph->entity->getType() == 'your_paragraph_type') {
$my_paragraph = $paragraph->entity;
}
}
if (!empty($my_paragraph)) {
print $my_paragraph->field_somefield->value;
print $my_paragraph->title->value; // यह काम नहीं करेगा!
}
else {
print "इस नोड में यह पैराग्राफ प्रकार नहीं है।";
}
पैराग्राफ का प्रकार प्राप्त करना:
$my_paragraph->getType();
हम इन उदाहरणों का उपयोग भविष्य में कस्टम मॉड्यूल लिखने में करेंगे जो hooks और entity objects के साथ काम करेंगे।