You are here

trait EntityDisplaySetupTrait in Video Embed Field 8

Same name and namespace in other branches
  1. 8.2 tests/src/Functional/EntityDisplaySetupTrait.php \Drupal\Tests\video_embed_field\Functional\EntityDisplaySetupTrait

A trait for manipulating entity display.

Hierarchy

2 files declare their use of EntityDisplaySetupTrait
ColorboxFormatterTest.php in tests/src/FunctionalJavascript/ColorboxFormatterTest.php
LazyLoadFormatterTest.php in tests/src/FunctionalJavascript/LazyLoadFormatterTest.php

File

tests/src/Functional/EntityDisplaySetupTrait.php, line 12

Namespace

Drupal\Tests\video_embed_field\Functional
View source
trait EntityDisplaySetupTrait {
  use ContentTypeCreationTrait;
  use NodeCreationTrait;

  /**
   * The field name.
   *
   * @var string
   */
  protected $fieldName;

  /**
   * The name of the content type.
   *
   * @var string
   */
  protected $contentTypeName;

  /**
   * The entity display.
   *
   * @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface
   */
  protected $entityDisplay;

  /**
   * The form display.
   *
   * @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface
   */
  protected $entityFormDisplay;

  /**
   * Setup the entity displays with required fields.
   */
  protected function setupEntityDisplays() {
    $this->fieldName = 'field_test_video_field';
    $this->contentTypeName = 'test_content_type_name';
    $this
      ->createContentType([
      'type' => $this->contentTypeName,
    ]);
    $field_storage = FieldStorageConfig::create([
      'field_name' => $this->fieldName,
      'entity_type' => 'node',
      'type' => 'video_embed_field',
      'settings' => [
        'allowed_providers' => [],
      ],
    ]);
    $field_storage
      ->save();
    FieldConfig::create([
      'field_storage' => $field_storage,
      'bundle' => $this->contentTypeName,
      'settings' => [],
    ])
      ->save();
    $this->entityDisplay = $this->container
      ->get('entity_display.repository')
      ->getViewDisplay('node', $this->contentTypeName, 'default');
    $this->entityFormDisplay = $this->container
      ->get('entity_display.repository')
      ->getFormDisplay('node', $this->contentTypeName, 'default');
  }

  /**
   * Set component settings for the display.
   *
   * @param string $type
   *   The component to change settings for.
   * @param array $settings
   *   The settings to use.
   */
  protected function setDisplayComponentSettings($type, $settings = []) {
    $this->entityDisplay
      ->setComponent($this->fieldName, [
      'type' => $type,
      'settings' => $settings,
    ])
      ->save();
  }

  /**
   * Set component settings for the form.
   *
   * @param string $type
   *   The component to change settings for.
   * @param array $settings
   *   The settings to use.
   */
  protected function setFormComponentSettings($type, $settings = []) {
    $this->entityFormDisplay
      ->setComponent($this->fieldName, [
      'type' => $type,
      'settings' => $settings,
    ])
      ->save();
  }

  /**
   * Create a video node using the video field.
   *
   * @param string $value
   *   The video URL to use for the field value.
   *
   * @return \Drupal\node\NodeInterface
   *   A node.
   */
  protected function createVideoNode($value) {
    return $this
      ->createNode([
      'type' => $this->contentTypeName,
      $this->fieldName => [
        [
          'value' => $value,
        ],
      ],
    ]);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ContentTypeCreationTrait::createContentType protected function Creates a custom content type based on default settings. 1
EntityDisplaySetupTrait::$contentTypeName protected property The name of the content type.
EntityDisplaySetupTrait::$entityDisplay protected property The entity display.
EntityDisplaySetupTrait::$entityFormDisplay protected property The form display.
EntityDisplaySetupTrait::$fieldName protected property The field name.
EntityDisplaySetupTrait::createVideoNode protected function Create a video node using the video field.
EntityDisplaySetupTrait::setDisplayComponentSettings protected function Set component settings for the display.
EntityDisplaySetupTrait::setFormComponentSettings protected function Set component settings for the form.
EntityDisplaySetupTrait::setupEntityDisplays protected function Setup the entity displays with required fields.
NodeCreationTrait::createNode protected function Creates a node based on default settings.
NodeCreationTrait::getNodeByTitle public function Get a node from the database based on its title.