buildMultiple example

/** * Tests the file_link field formatter. */
  public function testFormatterFileLink() {
    $entity_display = EntityViewDisplay::create([
      'targetEntityType' => 'file',
      'bundle' => 'file',
    ]);
    $entity_display->setComponent('filename', ['type' => 'file_link']);

    $build = $entity_display->buildMultiple($this->files)[0]['filename'][0];
    $this->assertEquals('file.png', $build['#title']);
    $this->assertEquals($this->fileUrlGenerator->generate('public://file.png')$build['#url']);
  }

  /** * Tests the file_link field formatter. */
  public function testFormatterFileUri() {
    $entity_display = EntityViewDisplay::create([
      'targetEntityType' => 'file',
      'bundle' => 'file',
    ]);


    // Persist the formatter object.     $this->plugins[$field_name] = $formatter;
    return $formatter;
  }

  /** * {@inheritdoc} */
  public function build(FieldableEntityInterface $entity) {
    $build = $this->buildMultiple([$entity]);
    return $build[0];
  }

  /** * {@inheritdoc} */
  public function buildMultiple(array $entities) {
    $build_list = [];
    foreach ($entities as $key => $entity) {
      $build_list[$key] = [];
    }

    
'targetEntityType' => $entity_type_id,
            'bundle' => $bundle,
            'status' => TRUE,
          ]);
          foreach ($display_fields['field_ids'] as $field) {
            $display->setComponent($field->definition['field_name'][
              'type' => $field->options['type'],
              'settings' => $field->options['settings'],
            ]);
          }
          // Let the display build the render array for the entities.           $display_build = $display->buildMultiple($bundle_entities);
          // Collect the field render arrays and index them using our internal           // row indexes and field IDs.           foreach ($display_build as $row_index => $entity_build) {
            foreach ($display_fields['field_ids'] as $field_id => $field) {
              $build[$row_index][$field_id] = !empty($entity_build[$field->definition['field_name']]) ? $entity_build[$field->definition['field_name']] : [];
            }
          }
        }
      }
    }

    
->setComponent($this->fieldTestData->field_name, [
        'type' => 'field_test_with_prepare_view',
      ]);

    // Create two entities.     $entity1 = EntityTest::create(['id' => 1, 'type' => 'entity_test']);
    $entity1->{$this->fieldTestData->field_name}->setValue($this->_generateTestFieldValues(1));
    $entity2 = EntityTest::create(['id' => 2, 'type' => 'entity_test']);
    $entity2->{$this->fieldTestData->field_name}->setValue($this->_generateTestFieldValues(1));

    // Run buildMultiple(), and check that the entities come out as expected.     $display->buildMultiple([$entity1$entity2]);
    $item1 = $entity1->{$this->fieldTestData->field_name}[0];
    $this->assertEquals($item1->value + 1, $item1->additional_formatter_value, 'Entity 1 ran through the prepareView() formatter method.');
    $item2 = $entity2->{$this->fieldTestData->field_name}[0];
    $this->assertEquals($item2->value + 1, $item2->additional_formatter_value, 'Entity 2 ran through the prepareView() formatter method.');
  }

  /** * Tests entity cache. * * Complements unit test coverage in * \Drupal\Tests\Core\Entity\Sql\SqlContentEntityStorageTest. */

  public function build(array $build) {
    $build_list = [$build];
    $build_list = $this->buildMultiple($build_list);
    return $build_list[0];
  }

  /** * Builds multiple entities' views; augments entity defaults. * * This function is assigned as a #pre_render callback in ::viewMultiple(). * * By delaying the building of an entity until the #pre_render processing in * \Drupal::service('renderer')->render(), the processing cost of assembling * an entity's renderable array is saved on cache-hit requests. * * @param array $build_list * A renderable array containing build information and context for an * entity view. * * @return array * The updated renderable array. * * @see \Drupal\Core\Render\RendererInterface::render() */


  /** * {@inheritdoc} */
  public function viewMultiple(array $entities = []$view_mode = 'full', $langcode = NULL) {
    $build_list = parent::viewMultiple($entities$view_mode$langcode);
    // Apply the buildMultiple() #pre_render callback immediately, to make     // bubbling of attributes and contextual links to the actual block work.     // @see \Drupal\block\BlockViewBuilder::buildBlock()     unset($build_list['#pre_render'][0]);
    return $this->buildMultiple($build_list);
  }

  /** * {@inheritdoc} */
  protected function getBuildDefaults(EntityInterface $entity$view_mode) {
    $build = parent::getBuildDefaults($entity$view_mode);
    // The content block will be rendered in the wrapped block template already     // and thus has no entity template itself.     unset($build['#theme']);
    return $build;
  }

  protected function isCustomMode() {
    return $this->getOriginalMode() === static::CUSTOM_MODE;
  }

  /** * {@inheritdoc} */
  public function buildMultiple(array $entities) {
    $build_list = parent::buildMultiple($entities);

    // Layout Builder can not be enabled for the '_custom' view mode that is     // used for on-the-fly rendering of fields in isolation from the entity.     if ($this->isCustomMode()) {
      return $build_list;
    }

    foreach ($entities as $id => $entity) {
      $build_list[$id]['_layout_builder'] = $this->buildSections($entity);

      // If there are any sections, remove all fields with configurable display
Home | Imprint | This part of the site doesn't use cookies.