Drupal 8 में Entity API का परिचय
Drupal 8 की Entity प्रणाली
Entity - यह टाइप किए गए क्लास होते हैं जिनके अपने मेथड्स होते हैं
| Generic methods |
$entity->id() |
| Entity type specific methods | $node->getTitle() |
पृष्ठभूमि
Entity System को Drupal 7 के विकास चक्र के अंत में पेश किया गया था, जिसमें Entity लोड करने के लिए बेसिक स्टैंडर्ड्स शामिल थे। शामिल किया गया entity.module ने API को और विस्तारित किया, जिसमें Entity को सेव और डिलीट करने और अन्य सुधारों के लिए सपोर्ट जोड़ा गया।
इनमें से अधिकांश सुधार अब Drupal 8 में शामिल हैं। Entity Validation अब अपने स्वयं के API में की जाती है (जो उदाहरण के लिए, REST से सेव की गई Entity की जाँच कर सकती है, न कि केवल फॉर्म से)।
दो विकल्प
Core में Entity प्रकार दो विकल्पों में आते हैं।
Configuration Entity
Configuration System का उपयोग करता है। यह ट्रांसलेशन को सपोर्ट करता है और इंस्टॉलेशन के लिए डिफ़ॉल्ट कॉन्फ़िगरेशन प्रदान कर सकता है। Configuration Entity को कॉन्फ़िगरेशन डेटाबेस टेबल में रोज़ के रूप में स्टोर किया जाता है।
Content Entity
यह बेस और कस्टम फ़ील्ड्स से मिलकर बना होता है, इसमें revisions हो सकते हैं और यह ट्रांसलेशन को सपोर्ट करता है। Content Entity को डेटाबेस की कस्टम टेबल्स में रोज़ के रूप में स्टोर किया जाता है। टेबल का नाम Entity के “id” पर आधारित होता है और कॉलम्स को Entity के “baseFieldDefinitions” मेथड द्वारा परिभाषित किया जाता है।
Bundles
Bundles किसी Entity प्रकार के विभिन्न वेरिएंट होते हैं। उदाहरण के लिए, Node Entity प्रकार में bundles अलग-अलग node प्रकार होते हैं, जैसे “Article” और “Page”।
आमतौर पर, bundle एक Configuration Entity द्वारा प्रदर्शित होता है, हालाँकि contrib मॉड्यूल्स में अन्य मॉडल भी हो सकते हैं। उदाहरण के लिए, Node प्रकार “article” स्वयं एक Configuration Entity होता है। Configuration यह जानकारी रखता है कि Content Entity प्रकारों में क्या अंतर है, जैसे सेटिंग्स और फ़ील्ड्स। जब आप bundle वाली Entity प्रकार बनाते हैं, तो आप Content Entity और Configuration Entity दोनों बनाते हैं। Content Entity कंटेंट डिटेल्स और ऑपरेशन्स को संभालता है, जबकि Configuration Entity Entity प्रकारों के बीच अंतर संभालता है।
Annotations
जब आप नया Entity प्रकार बनाते हैं, तो आपको Core में बनी Annotation प्रणाली का उपयोग करना होगा। Annotation क्लास के ऊपर docblock कमेंट्स की तरह दिखाई देते हैं, लेकिन इन्हें Drupal Core द्वारा पार्स और कैश किया जाता है। कई तरीकों से, Annotation Drupal 7 की पुरानी स्टाइल्स की जगह लेती है।
Annotation Parser
Annotation रनटाइम पर Annotation सिस्टम द्वारा पढ़े और पार्स किए जाते हैं। Drupal 8 Doctrine Annotation Parser का उपयोग करता है, जो Annotation को PHP द्वारा उपयोग किए जाने योग्य ऑब्जेक्ट्स में बदल देता है।
सिंटैक्स: Annotation का सिंटैक्स @ClassName() में घिरा होता है, और यह key/value जोड़ों पर आधारित होता है। इसमें arrays भी हो सकते हैं जिन्हें {} ब्रैकेट्स के साथ लिखा जाता है। टॉप-लेवल keys को कोट्स में नहीं रखा जाता, लेकिन array keys को रखना पड़ता है। प्रत्येक key/value जोड़ी को अलग लाइन पर लिखा जाता है और कॉमा से खत्म किया जाता है। कुछ विशेष फ़ंक्शंस को values पर लागू किया जा सकता है, जैसे @Translation().
Annotation सिंटैक्स का एक गैर-कार्यशील उदाहरण:
/**
* @ContentEntityType(
* id = "my_entity_type_id",
* label = @Translation("My entity type label"),
* example_pair = "this_examples_value",
* example_array = {
* "array_key" = "array_value",
* "some_other_key" = "some_other_value",
* },
* )
*/
सामान्य टॉप-लेवल Annotations
| Key = "Example Value" | विवरण | Entity Variant |
| id = "node", | Entity प्रकार का machine name। | Content & Config |
| label = @Translation("Node"), | Entity प्रकार का मानव-पठनीय नाम। | Content & Config |
| admin_permission = "administer nodes", | वह परमिशन जो Entity प्रकार को एडमिनिस्टर करने की अनुमति देता है। | Content & Config |
| bundle_label = @Translation("Content type"), | bundle प्रकार का मानव-पठनीय नाम। | Content |
| bundle_entity_type = "node_type", | यदि Content Entity bundle वाली है, तो यह संबंधित Configuration Entity का id होना चाहिए। | Content |
| base_table = "node", | Entity प्रकार की डेटाबेस टेबल का नाम। | Content |
| fieldable = TRUE, | क्या यह Entity प्रकार field UI से विस्तारित किया जा सकता है। | Content |
| field_ui_base_route = "entity.node_type.edit_form", | Field UI को entity के route से जोड़ने के लिए route name। | Content |
Handlers
Handlers को Annotation में array के रूप में परिभाषित किया जाता है। वे Entity को सपोर्ट करते हैं, जिससे Entity के कुछ हिस्सों को अलग PHP क्लासेस में सौंपा जाता है। ये क्लास उन हिस्सों को “handle” करते हैं।
Storage - Entity को लोड, सेव और डिलीट करता है। डिफ़ॉल्ट रूप से Content Entities Drupal\Core\Entity\Sql\SqlContentEntityStorage का उपयोग करते हैं और Configuration Entities Drupal\Core\Config\Entity\ConfigEntityStorage का। आप अपना storage handler बना सकते हैं।
उदाहरण: "storage" = "Drupal\node\NodeStorage",
Form - Add, edit और delete forms को handle करता है।
उदाहरण:
"form" = {
"add" = "Drupal\block\BlockForm",
"edit" = "Drupal\block\BlockForm",
"delete" = "Drupal\block\Form\BlockDeleteForm",
}
View builder - Entity को user को दिखाने के लिए output handle करता है।
उदाहरण: "view_builder" = "Drupal\node\NodeViewBuilder",
List builder - Entity सूची (admin पेज) को बनाता है।
उदाहरण: "list_builder" = "Drupal\node\NodeListBuilder",
Route provider - Routes जेनरेट करता है।
उदाहरण:
"route_provider" = {
"html" = "Drupal\Core\Entity\Routing\AdminHtmlRouteProvider",
}
Access - Access control handle करता है।
उदाहरण: "access" = "NodeAccessControlHandler",
Views data - Views integration देता है।
उदाहरण: "views_data" = "Drupal\node\NodeViewsData",
Storage schema - Database schema customization।
उदाहरण: "storage_schema" = "Drupal\node\NodeStorageSchema",
Translation - Translation हैंडलिंग।
उदाहरण: "translation" = "Drupal\node\NodeTranslationHandler",
पूर्ण handlers उदाहरण:
handlers = {
"view_builder" = "Drupal\Core\Entity\EntityViewBuilder",
"list_builder" = "Drupal\Core\Entity\EntityListBuilder",
"access" = "Drupal\Core\Entity\EntityAccessControlHandler",
"views_data" = "Drupal\views\EntityViewsData",
"storage" = "Drupal\Core\Entity\Sql\SqlContentEntityStorage",
"storage_schema" = "Drupal\Core\Entity\Sql\SqlContentEntityStorageSchema",
"translation" = "Drupal\content_translation\ContentTranslationHandler",
"form" = {
"default" = "Drupal\Core\Entity\ContentEntityForm",
"add" = "Drupal\Core\Entity\ContentEntityForm",
"edit" = "Drupal\Core\Entity\ContentEntityForm",
"delete" = "Drupal\Core\Entity\ContentEntityDeleteForm",
},
"route_provider" = {
"html" = "Drupal\Core\Entity\Routing\AdminHtmlRouteProvider",
},
},
Links
Links Annotation में array के रूप में परिभाषित किए जाते हैं। यह Entity प्रकार या Entity instance के लिए routes को परिभाषित करते हैं।
id = "node",
handlers = {
"route_provider" = {
"html" = "Drupal\Core\Entity\Routing\AdminHtmlRouteProvider"
}
},
links = {
"canonical" = "/node/{node}",
"add-page" = "/node/add",
"add-form" = "/node/add/{node_type}",
"edit-form" = "/node/{node}/edit",
"delete-form" = "/node/{node}/delete",
"collection" = "/admin/content",
},
Links और route_provider
ये links और route_provider साथ में named routes बनाते हैं:
| लिंक कुंजी | Route नाम | उदाहरण URI | विवरण |
| canonical | entity.node.canonical | /node/1 | किसी node को देखें |
| add-page | entity.node.add_page | /node/add | कौन सा node जोड़ना है चुनें |
| add-form | entity.node.add_form | /node/add/article | Node जोड़ें (bundle विशेष) |
| edit-form | entity.node.edit_form | /node/1/edit | Node एडिट करें |
| delete-form | entity.node.delete_form | /node/1/delete | Node डिलीट करें |
| collection | entity.node.collection | /admin/content | सभी nodes की सूची |
लिंक का उपयोग
$view_url_object = $entity->toUrl(); // Default 'canonical'
$edit_url_string = $entity->toUrl('edit-form')->toString();
संदर्भ:
- Entity API - जेनरेटेड डॉक्यूमेंटेशन।
- कस्टम Content Entity बनाना - एक सरल उदाहरण।
- Drupal 8 में Content Entity प्रकार बनाना - advanced उदाहरण।
- Drupal 8 में Configuration Entity प्रकार बनाना - advanced उदाहरण।
- [External] Entity Type Walkthrough - एक generic Entity प्रकार का डॉक्यूमेंटेशन।