7.2 加载字段的例子

例子模块

龸龴龱龯龫龩龥龡龝龙龗龓龐龏龩龥龋龊龆node齺龓齸齵齲齰龩齮齬齩齥

齣齟龓齜齘龯龫龩龥龋龊龓body 齎齋齉齆龩齮齃齁齥

齀鼿龓龸龴龯龫鼽龥龙龗龓龐龏鼺龩龋龊齥

龯龫鼸鼵 field_example.info.yml龆鼛鼙鼗1齺 鼓

龯龫鼸鼵 field_example.routing.yml龆鼛鼙鼗2齺:

代码1:

name: Field Example
type: module
description: Example showing how to use fields
core: 8.x
package: Examples

代码2:

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' => [],
    ],
  ],
];

评论 (写第一个评论)