由于上次写的Entity扩展改起来比较复杂。并且也不是太好用。所以这次提供了一个简单的模块来实现这个功能,已经上传到Drupal.org
https://www.drupal.org/project/entity_expand
2023-06-27
2023-04-09
在使用entity中有些事情比较繁琐, 比如
1. 获取关联的entity
$entity->get('field')->entity->toArray()
这里有一个弊端.如果entity已经被删除了或者entity不存在都会报错.
2. 获取多值的target_id, 需要有多步操作.
$ids = [];
if (!$entity->get('field')->isEmpty()) {
$ids = $entity->get('field')->getValue();
$ids = array_column($ids, 'target_id');
}
3. 获取多值的广本字段.也需要有多步操作
$values = [];
if (!$entity->get('field')->isEmpty()) {
$values = $entity->get('field')->getValue();
$values = array_column($values, 'value');
}
4. 获取lis字段的label
$value = '';
if ($entity->get('field')->isEmpty()) {
$allow_values = $entity->get('field')->getSetting('allowed_values');
$value = $allow_values[$entity->get('field')->value];
}
以下是一个简单扩展
<?php
namespace Drupal\mymodule;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Field\FieldItemListInterface;
/**
* entity helper.
*
* Class Entity
*/
class Entity {
/**
* @var EntityInterface
*/
private EntityInterface $entity;
/**
* init entity.
*
* Entity constructor.
* @param EntityInterface $entity
*/
public function __construct(EntityInterface $entity) {
$this->entity = $entity;
}
/**
* get field use custom.
*
* @param $field
* @return FieldItem
*/
public function get($field):FieldItem {
return new FieldItem($this->entity, $field);
}
/**
* bundle key
* node key is type
* profile key is profile_type
* @return false|string
*/
public function bundleKey() {
return $this->entity->getEntityType()->getKey('bundle');
}
/**
* call entity old functions.
*
* @param $name
* @param $arguments
* @return false|mixed
*/
public function __call($name, $arguments)
{
if (method_exists($this->entity, $name)) {
return call_user_func_array(array(
$this->entity,
$name
), $arguments);
}
return false;
}
/**
* call entity old property.
*
* @param $name
* @return FieldItem
*/
public function __get($name) {
return new FieldItem($this->entity, $name);
}
}
/**
* entity field item.
* Class FieldItem
* @package Drupal\dyniva_helper
*/
class FieldItem {
/**
* @var FieldItemListInterface
*/
private FieldItemListInterface $field;
/**
* FieldItem constructor.
* @param EntityInterface $entity
* @param $field
*/
public function __construct(EntityInterface $entity, $field) {
$this->field = $entity->get($field);
}
/**
* set call field old functions.
*
* @param $name
* @param $arguments
* @return false|mixed
*/
public function __call($name, $arguments) {
if (method_exists($this->field, $name)) {
return call_user_func_array(array(
$this->field,
$name
), $arguments);
}
return false;
}
/**
* set call field old property.
*
* @param $name
* @return mixed
*/
public function __get($name) {
return $this->field->{$name};
}
public function emptyValue($default = '') {
if (!$this->field->isEmpty()) {
return $this->field->getString();
}
return $default;
}
/**
* get select field allow values.
*
* @return mixed
*/
public function getAllowValues() {
return $this->field->getSetting('allowed_values');
}
public function selectLabel() {
$allowvalues = $this->getAllowValues();
return $allowvalues[$this->field->value] ?? '';
}
/**
* get ref entity.
*
* @return false|EntityInterface
*/
public function ref() {
if (!$this->field->isEmpty() && $this->field->entity) {
return new Entity($this->field->entity);
}
return false;
}
/**
* get refs entity.
*
* @return false|array
*/
public function refs() {
if (!$this->field->isEmpty()) {
$entitys = $this->field->referencedEntities();
foreach ($entitys as $key => $entity) {
$entitys[$key] = new Entity($entity);
}
return $entitys;
}
return [];
}
/**
* get field targets.
*
* @return array
*/
public function targets() {
if ($this->field->isEmpty()) {
return [];
}
return array_column($this->field->getValue(), 'target_id');
}
/**
* get field values.
*
* @return array
*/
public function values() {
if ($this->field->isEmpty()) {
return [];
}
return array_column($this->field->getValue(), 'value');
}
}
使用方法
$node = node_load(1);
$entity = new \Drupal\mymodule\Entity($node);
$entity->uid->refs(); 获取多个关联entity
$entity->uid->ref(); 获取单个关联的entity
$entity->uid->targets(); 获取多个target_id
$entity->get('titlte')->values(); 获取多个value
$entity->get('select_field')->selectLabel(); 获取list select字段的label. 经常在使用value的时候只会获取Key