原文地址:
https://docs.acquia.com/articles/drupal-8-loading-fields-examples
例子模块
我们将创建一个例子模块,加载一个节点(node),输出它的一些信息。首先,至少创建一个节点,body 字段填写一些内容。然后,我们创建这个模块,加载单一节点。
创建文件 field_example.info.yml :
name: Field Example type: module description: Example showing how to use fields core: 8.x package: Examples
创建文件 field_example.routing.yml :
field_example.simple: path: 'examples/field-example/simple' defaults: _controller: '\Drupal\field_example\Controller\FieldExampleDefaultController::simple' requirements: _access: 'TRUE'
创建控制器文件 src/Controller/FieldExampleDefaultController.php :
<?php /** * @file * Contains \Drupal\field_example\Controller\FieldExampleDefaultController. */ namespace Drupal\field_example\Controller; use Drupal\Core\Controller\ControllerBase; use Drupal\Core\Entity\EntityManagerInterface; use Drupal\Core\Entity\Query\QueryFactory; use Symfony\Component\DependencyInjection\ContainerInterface; /** * Controller routines for field example routes. */ class FieldExampleDefaultController extends ControllerBase { /** * @var \Drupal\Core\Entity\EntityManagerInterface */ protected $entityManager; /** * @var \Drupal\Core\Entity\Query\QueryFactory */ protected $queryFactory; /** * @param \Drupal\Core\Entity\EntityManagerInterface $entityManager * @param \Drupal\Core\Entity\Query\QueryFactory $queryFactory */ public function __construct(EntityManagerInterface $entityManager, QueryFactory $queryFactory) { $this->entityManager = $entityManager; $this->queryFactory = $queryFactory; } /** * @param \Symfony\Component\DependencyInjection\ContainerInterface $container * @return static */ public static function create(ContainerInterface $container) { return new static( $container->get('entity.manager'), $container->get('entity.query') ); } public function simple() { /** * We need to get one node with a body field. */ /** * @var $query \Drupal\Core\Entity\Query\QueryInterface */ $query = $this->queryFactory->get('node'); // We just want some nodes. $nids = $query->execute(); // If there are no nodes, we exit. if (count($nids) < 1) { return ['#markup' => $this->t('No nodes with a body field.')]; } /** * @var $entity_storage \Drupal\Core\Entity\ContentEntityStorageBase */ $entity_storage = $this->entityManager->getStorage('node'); /** * @var $entity \Drupal\Core\Entity\Entity */ $entity = $entity_storage->load(array_values($nids)[0]); // We will get the values from here. return [ '#markup' => 'placeholder', ]; } }
创建节点后,我们来取得 body 字段的值,并使用之前讲到的方式改变它。
/** * @var $field \Drupal\Core\Field\FieldItemList */ $body_field = $entity->get('body'); // If we want a value off the first item, we can use a magic method __get() // Which is sometimes easier to use. $simple_value = $body_field->value; // We get a array of items for this field. // Unlike Drupal 7, there are no language keys. $field_value = $body_field->getValue(); // We will set the summary to the value. // We don't need to update the entity because the field setValue does that already. $body_field->summary = $body_field->value; // or $field_value[0]['summary'] = $field_value[0]['value']; $body_field->setValue($field_value); /** * @var $definition \Drupal\Core\Field\BaseFieldDefinition */ $definition = $body_field->getFieldDefinition(); $field_type = $definition->get('field_type'); return [ 'field_type' => [ '#markup' => 'Field Type: ' . $field_type, '#theme_wrappers' => [ 'container' => [], ], ], 'field_value' => [ '#markup' => 'Field Value: ' . $body_field->value, '#theme_wrappers' => [ 'container' => [], ], ], ];
回顾
我们学习了长、短两种访问、编辑字段的方法。从 Drupal 7 的主要变化是我们不再直接访问字段,而是要通过实体访问字段。另外现在统一使用 FieldItemList 访问字段。
某些情况下,你需要访问 field 对象的 Field Storage 系统。