Hướng dẫn lấy value từ Search Api View thông qua EntityAdapter Drupal 8

Hướng dẫn lấy value từ Search Api View thông qua EntityAdapter Drupal 8

Tham khảo 1

diff --git a/src/Plugin/search_api/datasource/ContentEntity.php b/src/Plugin/search_api/datasource/ContentEntity.php
index 23865b7..6e3357a 100644
--- a/src/Plugin/search_api/datasource/ContentEntity.php
+++ b/src/Plugin/search_api/datasource/ContentEntity.php
@@ -10,7 +10,6 @@
 use Drupal\Core\Entity\EntityInterface;
 use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
 use Drupal\Core\Entity\EntityTypeManagerInterface;
-use Drupal\Core\Entity\Plugin\DataType\EntityAdapter;
 use Drupal\Core\Entity\TypedData\EntityDataDefinitionInterface;
 use Drupal\Core\Form\FormStateInterface;
 use Drupal\Core\Language\LanguageInterface;
@@ -582,11 +581,26 @@ public function submitConfigurationForm(array &$form, FormStateInterface $form_s
   }
 
   /**
+   * Retrieves the entity from a search item.
+   *
+   * @param \Drupal\Core\TypedData\ComplexDataInterface $item
+   *   An item of this datasource's type.
+   *
+   * @return \Drupal\Core\Entity\EntityInterface|null
+   *   The entity object represented by that item, or NULL if none could be
+   *   found.
+   */
+  protected function getEntity(ComplexDataInterface $item) {
+    $value = $item->getValue();
+    return $value instanceof EntityInterface ? $value : NULL;
+  }
+
+  /**
    * {@inheritdoc}
    */
   public function getItemId(ComplexDataInterface $item) {
-    if ($item instanceof EntityAdapter) {
-      return $item->getValue()->id() . ':' . $item->getValue()->language()->getId();
+    if ($entity = $this->getEntity($item)) {
+      return $entity->id() . ':' . $entity->language()->getId();
     }
     return NULL;
   }
@@ -595,8 +609,8 @@ public function getItemId(ComplexDataInterface $item) {
    * {@inheritdoc}
    */
   public function getItemLabel(ComplexDataInterface $item) {
-    if ($item instanceof EntityAdapter) {
-      return $item->getValue()->label();
+    if ($entity = $this->getEntity($item)) {
+      return $entity->label();
     }
     return NULL;
   }
@@ -605,8 +619,8 @@ public function getItemLabel(ComplexDataInterface $item) {
    * {@inheritdoc}
    */
   public function getItemBundle(ComplexDataInterface $item) {
-    if ($item instanceof EntityAdapter) {
-      return $item->getValue()->bundle();
+    if ($entity = $this->getEntity($item)) {
+      return $entity->bundle();
     }
     return NULL;
   }
@@ -615,8 +629,7 @@ public function getItemBundle(ComplexDataInterface $item) {
    * {@inheritdoc}
    */
   public function getItemUrl(ComplexDataInterface $item) {
-    if ($item instanceof EntityAdapter) {
-      $entity = $item->getValue();
+    if ($entity = $this->getEntity($item)) {
       if ($entity->hasLinkTemplate('canonical')) {
         return $entity->toUrl('canonical');
       }
@@ -795,8 +808,7 @@ public function getViewModes($bundle = NULL) {
    */
   public function viewItem(ComplexDataInterface $item, $view_mode, $langcode = NULL) {
     try {
-      if ($item instanceof EntityAdapter) {
-        $entity = $item->getValue();
+      if ($entity = $this->getEntity($item)) {
         $langcode = $langcode ?: $entity->language()->getId();
         return $this->getEntityTypeManager()->getViewBuilder($this->getEntityTypeId())->view($entity, $view_mode, $langcode);
       }
@@ -820,8 +832,8 @@ public function viewMultipleItems(array $items, $view_mode, $langcode = NULL) {
       if (isset($langcode)) {
         $entities = array();
         foreach ($items as $i => $item) {
-          if ($item instanceof EntityAdapter) {
-            $entities[$i] = $item->getValue();
+          if ($entity = $this->getEntity($item)) {
+            $entities[$i] = $entity;
           }
         }
         if ($entities) {

Tham khảo thứ 2

diff --git a/src/Plugin/search_api/datasource/ContentEntity.php b/src/Plugin/search_api/datasource/ContentEntity.php
index a8a4919..6e3357a 100644
--- a/src/Plugin/search_api/datasource/ContentEntity.php
+++ b/src/Plugin/search_api/datasource/ContentEntity.php
@@ -10,7 +10,6 @@
 use Drupal\Core\Entity\EntityInterface;
 use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
 use Drupal\Core\Entity\EntityTypeManagerInterface;
-use Drupal\Core\Entity\Plugin\DataType\EntityAdapter;
 use Drupal\Core\Entity\TypedData\EntityDataDefinitionInterface;
 use Drupal\Core\Form\FormStateInterface;
 use Drupal\Core\Language\LanguageInterface;
@@ -833,8 +832,8 @@ public function viewMultipleItems(array $items, $view_mode, $langcode = NULL) {
       if (isset($langcode)) {
         $entities = array();
         foreach ($items as $i => $item) {
-          if ($item instanceof EntityAdapter) {
-            $entities[$i] = $item->getValue();
+          if ($entity = $this->getEntity($item)) {
+            $entities[$i] = $entity;
           }
         }
         if ($entities) {
Bạn thấy bài viết này như thế nào?: 
Average: 10 (1 vote)
Ảnh của Tommy Tran

Tommy owner Express Magazine

Drupal Developer having 9+ year experience, implementation and having strong knowledge of technical specifications, workflow development. Ability to perform effectively and efficiently in team and individually. Always enthusiastic and interseted to study new technologies

  • Skype ID: tthanhthuy

Bình luận (0)

 

Add Comment

Filtered HTML

  • Các địa chỉ web và email sẽ tự động được chuyển sang dạng liên kết.
  • Các thẻ HTML được chấp nhận: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Tự động ngắt dòng và đoạn văn.

Plain text

  • No HTML tags allowed.
  • Các địa chỉ web và email sẽ tự động được chuyển sang dạng liên kết.
  • Tự động ngắt dòng và đoạn văn.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.

Advertisement

 

jobsora

Dich vu khu trung tphcm

Dich vu diet chuot tphcm

Dich vu diet con trung

Quảng Cáo Bài Viết

 
Showbiz - hot girl bánh tráng trộn nói không

Showbiz - hot girl bánh tráng trộn nói không

Cuối tháng 11/2013, Bảo Chi - cô nàng hot girl bán bánh tráng trộn bất ngờ nổi lên như một hiện tượng và xuất hiện nhiều trên các phương tiện thông tin đại chúng. Điều khiến Chi được mọi người tò mò vì một cô chủ hàng bánh tráng trộn bình dân lại có thân hình khá chuẩn, gương mặt đáng yêu, làn da trắng trẻo, mịn màng đặc trưng của con gái vùng núi Đà Lạt.

Facebook sắp “mở rộng” tính năng Subscribe

Facebook sắp “mở rộng” tính năng Subscribe

Thời gian tới, Facebook sẽ tung ra một nút Subscribe mới.

Khóa học Drupal 60 ngày từ số zero với Acquia Lightning

Khóa học Drupal 60 ngày từ số zero với Acquia Lightning

To build great sites and digital experiences, you need easy-to-use tools for rapid development, simple page building, drag-and-drop widgets for rich media and content

Công ty diệt chuột T&C

 

Diet con trung