getTargetType example


  protected function assertEntity(string $id, string $label, string $entity_type): void {
    /** @var \Drupal\Core\Entity\EntityViewModeInterface $view_mode */
    $view_mode = EntityViewMode::load($id);
    $this->assertInstanceOf(EntityViewModeInterface::class$view_mode);
    $this->assertSame($label$view_mode->label());
    $this->assertSame($entity_type$view_mode->getTargetType());
  }

  /** * Tests migration of D7 view mode variables to D8 config entities. */
  public function testMigration() {
    $this->assertEntity('comment.full', 'Full', 'comment');
    $this->assertEntity('node.teaser', 'Teaser', 'node');
    $this->assertEntity('node.full', 'Full', 'node');
    $this->assertEntity('node.custom', 'custom', 'node');
    $this->assertEntity('user.full', 'Full', 'user');
  }
'#type' => 'textfield',
      '#title' => $this->t('Name'),
      '#maxlength' => 100,
      '#default_value' => $this->entity->label(),
    ];

    $form['id'] = [
      '#type' => 'machine_name',
      '#description' => $this->t('A unique machine-readable name. Can only contain lowercase letters, numbers, and underscores.'),
      '#disabled' => !$this->entity->isNew(),
      '#default_value' => $this->entity->id(),
      '#field_prefix' => $this->entity->isNew() ? $this->entity->getTargetType() . '.' : '',
      '#machine_name' => [
        'exists' => [$this, 'exists'],
        'replace_pattern' => '[^a-z0-9_.]+',
      ],
    ];

    return $form;
  }

  /** * Determines if the display mode already exists. * * @param string|int $entity_id * The entity ID. * @param array $element * The form element. * * @return bool * TRUE if the display mode exists, FALSE otherwise. */
public function buildRow(EntityInterface $entity) {
    $row['label'] = $entity->label();
    return $row + parent::buildRow($entity);
  }

  /** * {@inheritdoc} */
  public function load() {
    $entities = [];
    foreach (parent::load() as $entity) {
      $entities[$entity->getTargetType()][] = $entity;
    }
    return $entities;
  }

  /** * {@inheritdoc} */
  public function render() {
    $build = [];
    foreach ($this->load() as $entity_type => $entities) {
      if (!isset($this->entityTypes[$entity_type])) {
        

  protected $cache = TRUE;

  /** * {@inheritdoc} */
  public static function sort(ConfigEntityInterface $a, ConfigEntityInterface $b) {
    /** @var \Drupal\Core\Entity\EntityDisplayModeInterface $a */
    /** @var \Drupal\Core\Entity\EntityDisplayModeInterface $b */
    // Sort by the type of entity the view mode is used for.     $a_type = $a->getTargetType();
    $b_type = $b->getTargetType();
    $type_order = strnatcasecmp($a_type$b_type);
    return $type_order != 0 ? $type_order : parent::sort($a$b);
  }

  /** * {@inheritdoc} */
  public function getTargetType() {
    return $this->targetEntityType;
  }

  
->getMock();

    // A test value.     $target = 'test_target_type';

    // Gain access to the protected property.     $property = new \ReflectionProperty($mock, 'targetEntityType');
    // Set the property to a known state.     $property->setValue($mock$target);

    // Get the target type.     $value = $mock->getTargetType($target);

    // Test the outcome.     $this->assertEquals($value$property->getValue($mock));
  }

}

    $this->submitForm($edit, 'Save');
    $this->assertSession()->pageTextContains("Saved the {$edit['label']} view mode.");

    // Test editing the view mode.     $this->drupalGet('admin/structure/display-modes/view/manage/entity_test.' . $edit['id']);

    // Test that the link templates added by field_ui_entity_type_build() are     // generating valid routes.     $view_mode = EntityViewMode::load('entity_test.' . $edit['id']);
    $this->assertEquals(Url::fromRoute('entity.entity_view_mode.collection')->toString()$view_mode->toUrl('collection')->toString());
    $this->assertEquals(Url::fromRoute('entity.entity_view_mode.add_form', ['entity_type_id' => $view_mode->getTargetType()])->toString()$view_mode->toUrl('add-form')->toString());
    $this->assertEquals(Url::fromRoute('entity.entity_view_mode.edit_form', ['entity_view_mode' => $view_mode->id()])->toString()$view_mode->toUrl('edit-form')->toString());
    $this->assertEquals(Url::fromRoute('entity.entity_view_mode.delete_form', ['entity_view_mode' => $view_mode->id()])->toString()$view_mode->toUrl('delete-form')->toString());

    // Test deleting the view mode.     $this->clickLink('Delete');
    $this->assertSession()->pageTextContains("Are you sure you want to delete the view mode {$edit['label']}?");
    $this->submitForm([], 'Delete');
    $this->assertSession()->pageTextContains("The view mode {$edit['label']} has been deleted.");
  }

  /** * Tests the EntityFormMode user interface. */
Home | Imprint | This part of the site doesn't use cookies.