drupalBuildEntityView example

// Create a node to view.     $settings = [
      // cSpell:disable-next-line       'body' => [['value' => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam vitae arcu at leo cursus laoreet. Curabitur dui tortor, adipiscing malesuada tempor in, bibendum ac diam. Cras non tellus a libero pellentesque condimentum. What is a Drupalism? Suspendisse ac lacus libero. Ut non est vel nisl faucibus interdum nec sed leo. Pellentesque sem risus, vulputate eu semper eget, auctor in libero. Ut fermentum est vitae metus convallis scelerisque. Phasellus pellentesque rhoncus tellus, eu dignissim purus posuere id. Quisque eu fringilla ligula. Morbi ullamcorper, lorem et mattis egestas, tortor neque pretium velit, eget eleifend odio turpis eu purus. Donec vitae metus quis leo pretium tincidunt a pulvinar sem. Morbi adipiscing laoreet mauris vel placerat. Nullam elementum, nisl sit amet scelerisque malesuada, dolor nunc hendrerit quam, eu ultrices erat est in orci. Curabitur feugiat egestas nisl sed accumsan.']],
      'promote' => 1,
    ];
    $node = $this->drupalCreateNode($settings);
    $this->assertNotEmpty(Node::load($node->id()), 'Node created.');

    // Render the node as a teaser.     $content = $this->drupalBuildEntityView($node, 'teaser');
    $this->assertLessThan(600, strlen($content['body'][0]['#markup']));
    $this->setRawContent($renderer->renderRoot($content));
    // The string 'What is a Drupalism?' is between the 200th and 600th     // characters of the node body, so it should be included if the summary is     // 600 characters long.     $expected = 'What is a Drupalism?';
    $this->assertRaw($expected);

    // Change the teaser length for "Basic page" content type.     $display = \Drupal::service('entity_display.repository')
      ->getViewDisplay('node', $node->getType(), 'teaser');
    
// Confirm the defaults are present on the page field storage settings form.     $this->drupalGet("admin/structure/types/manage/page/fields/$field_id/storage");
    $this->assertSession()->hiddenFieldValueEquals('settings[default_image][uuid][fids]', $default_images['field_storage']->id());
    // Confirm the defaults are present on the page field edit form.     $field2_id = $field2->id();
    $this->drupalGet("admin/structure/types/manage/page/fields/$field2_id");
    $this->assertSession()->hiddenFieldValueEquals('settings[default_image][uuid][fids]', $default_images['field2']->id());

    // Confirm that the image default is shown for a new article node.     $article = $this->drupalCreateNode(['type' => 'article']);
    $article_built = $this->drupalBuildEntityView($article);
    $this->assertEquals($default_images['field']->id()$article_built[$field_name][0]['#item']->target_id, new FormattableMarkup('A new article node without an image has the expected default image file ID of @fid.', ['@fid' => $default_images['field']->id()]));

    // Also check that the field renders without warnings when the label is     // hidden.     EntityViewDisplay::load('node.article.default')
      ->setComponent($field_name['label' => 'hidden', 'type' => 'image'])
      ->save();
    $this->drupalGet('node/' . $article->id());

    // Confirm that the image default is shown for a new page node.     $page = $this->drupalCreateNode(['type' => 'page']);
    
Home | Imprint | This part of the site doesn't use cookies.