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

 
iTunes 11 được thiết kế thoáng và rộng rãi

iTunes 11 được thiết kế thoáng và rộng rãi

Ngoài những thay đổi về diện mạo bên ngoài, chương trình quản lí, chơi nhạc và các thiết bị của Apple còn được bổ sung thêm tính năng cũng như tích hợp công nghệ điện toán đám mây mạnh hơn.

Apple: Gã khổng lồ “tự ăn thịt chính mình”

Apple: Gã khổng lồ “tự ăn thịt chính mình”

Con đường trở thành công ty giá trị nhất hành tinh của Apple với phương châm thiết kế sản phẩm là yếu tố hàng đầu và chiến lược tự "ăn thịt" chính mình.

Giới thiệu TTR Configurable Widget in Drupal 7

Giới thiệu TTR Configurable Widget in Drupal 7

Microserve is proud to announce the submission of another Drupal contrib module – TTR Configurable Widget. The module extends upon the Taxonomy Term Reference field by adding further customisable options to use rather than the default ‘Checkboxes’ and ‘Select List’.

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

 

Diet con trung