getLinkTemplates example

$this->entityLastInstalledSchemaRepository = $entity_last_installed_schema_repository;
  }

  /** * {@inheritdoc} */
  public function processDefinition(&$definition$plugin_id) {
    /** @var \Drupal\Core\Entity\EntityTypeInterface $definition */
    parent::processDefinition($definition$plugin_id);

    // All link templates must have a leading slash.     foreach ((array) $definition->getLinkTemplates() as $link_relation_name => $link_template) {
      if ($link_template[0] != '/') {
        throw new InvalidLinkTemplateException("Link template '$link_relation_name' for entity type '$plugin_id' must start with a leading slash, the current link template is '$link_template'");
      }
    }
  }

  /** * {@inheritdoc} */
  protected function findDefinitions() {
    $definitions = $this->getDiscovery()->getDefinitions();
    
/** * {@inheritdoc} */
  public function getLinkTemplates() {
    return $this->links;
  }

  /** * {@inheritdoc} */
  public function getLinkTemplate($key) {
    $links = $this->getLinkTemplates();
    return $links[$key] ?? FALSE;
  }

  /** * {@inheritdoc} */
  public function hasLinkTemplate($key) {
    $links = $this->getLinkTemplates();
    return isset($links[$key]);
  }

  
/** * Sets up the entity type manager to be tested. * * @param \Drupal\Core\Entity\EntityTypeInterface[]|\Prophecy\Prophecy\ProphecyInterface[] $definitions * (optional) An array of entity type definitions. */
  protected function setUpEntityTypeDefinitions($definitions = []) {
    foreach ($definitions as $key => $entity_type) {
      // \Drupal\Core\Entity\EntityTypeInterface::getLinkTemplates() is called       // by \Drupal\Core\Entity\EntityTypeManager::processDefinition() so it must       // always be mocked.       $entity_type->getLinkTemplates()->willReturn([]);

      // Give the entity type a legitimate class to return.       $entity_type->getClass()->willReturn(EntityInterface::class);

      $definitions[$key] = $entity_type->reveal();
    }

    $this->entityTypeManager->getDefinition(Argument::cetera())
      ->will(function D$args) use ($definitions) {
        $entity_type_id = $args[0];
        $exception_on_invalid = $args[1];
        
/** * Sets up the entity type manager to be tested. * * @param \Drupal\Core\Entity\EntityTypeInterface[]|\Prophecy\Prophecy\ProphecyInterface[] $definitions * (optional) An array of entity type definitions. */
  protected function setUpEntityTypeDefinitions($definitions = []) {
    foreach ($definitions as $key => $entity_type) {
      // \Drupal\Core\Entity\EntityTypeInterface::getLinkTemplates() is called       // by \Drupal\Core\Entity\EntitTypeManager::processDefinition() so it must       // always be mocked.       $entity_type->getLinkTemplates()->willReturn([]);

      $definitions[$key] = $entity_type->reveal();
    }

    $this->entityTypeManager->getDefinition(Argument::cetera())
      ->will(function D$args) use ($definitions) {
        $entity_type_id = $args[0];
        $exception_on_invalid = $args[1];
        if (isset($definitions[$entity_type_id])) {
          return $definitions[$entity_type_id];
        }
        
// Not only assert the normalization, also assert deserialization of the     // response results in the expected object.     // Note: deserialization of the XML format is not supported, so only test     // this for other formats.     if (static::$format !== 'xml') {
      $unserialized = $this->serializer->deserialize((string) $response->getBody()get_class($this->entity)static::$format);
      $this->assertSame($unserialized->uuid()$this->entity->uuid());

    }
    // Finally, assert that the expected 'Link' headers are present.     if ($this->entity->getEntityType()->getLinkTemplates()) {
      $this->assertArrayHasKey('Link', $response->getHeaders());
      $link_relation_type_manager = $this->container->get('plugin.manager.link_relation_type');
      $expected_link_relation_headers = array_map(function D$relation_name) use ($link_relation_type_manager) {
        $link_relation_type = $link_relation_type_manager->createInstance($relation_name);
        return $link_relation_type->isRegistered()
          ? $link_relation_type->getRegisteredName()
          : $link_relation_type->getExtensionUri();
      }array_keys($this->entity->getEntityType()->getLinkTemplates()));
      $parse_rel_from_link_header = function D$value) {
        $matches = [];
        if (preg_match('/rel="([^"]+)"/', $value$matches) === 1) {
          
/** * Sets up the entity type manager to be tested. * * @param \Drupal\Core\Entity\EntityTypeInterface[]|\Prophecy\Prophecy\ProphecyInterface[] $definitions * (optional) An array of entity type definitions. */
  protected function setUpEntityTypeManager($definitions = []) {
    foreach ($definitions as $key => $entity_type) {
      // \Drupal\Core\Entity\EntityTypeInterface::getLinkTemplates() is called       // by \Drupal\Core\Entity\EntityTypeManager::processDefinition() so it must       // always be mocked.       $entity_type->getLinkTemplates()->willReturn([]);

      $definitions[$key] = $entity_type->reveal();
    }

    $this->entityTypeManager->getDefinition(Argument::cetera())
      ->will(function D$args) use ($definitions) {
        $entity_type_id = $args[0];
        $exception_on_invalid = $args[1];
        if (isset($definitions[$entity_type_id])) {
          return $definitions[$entity_type_id];
        }
        
/** * Sets up the entity type manager to be tested. * * @param \Drupal\Core\Entity\EntityTypeInterface[]|\Prophecy\Prophecy\ProphecyInterface[] $definitions * (optional) An array of entity type definitions. */
  protected function setUpEntityTypeDefinitions($definitions = []) {
    foreach ($definitions as $key => $entity_type) {
      // \Drupal\Core\Entity\EntityTypeInterface::getLinkTemplates() is called       // by \Drupal\Core\Entity\EntityTypeManager::processDefinition() so it must       // always be mocked.       $entity_type->getLinkTemplates()->willReturn([]);

      $definitions[$key] = $entity_type->reveal();
    }

    $this->entityTypeManager->getDefinition(Argument::type('string'))
      ->will(function D$args) use ($definitions) {
        if (isset($definitions[$args[0]])) {
          return $definitions[$args[0]];
        }
        throw new PluginNotFoundException($args[0]);
      });
    
$methods = array_merge($methods['getEntityType', 'entityTypeBundleInfo']);

    // Prophecy does not allow prophesizing abstract classes while actually     // calling their code. We use Prophecy below because that allows us to     // add method prophecies later while still revealing the prophecy now.     $entity = $this->getMockBuilder($class)
      ->setConstructorArgs([$values$this->entityTypeId])
      ->onlyMethods($methods)
      ->getMockForAbstractClass();

    $this->entityType = $this->prophesize(EntityTypeInterface::class);
    $this->entityType->getLinkTemplates()->willReturn([]);
    $this->entityType->getKey('langcode')->willReturn(FALSE);
    $entity->method('getEntityType')->willReturn($this->entityType->reveal());

    $this->entityTypeBundleInfo = $this->prophesize(EntityTypeBundleInfoInterface::class);
    $entity->method('entityTypeBundleInfo')->willReturn($this->entityTypeBundleInfo->reveal());

    return $entity;
  }

  /** * Asserts that a given URL object matches the expectations. * * @param string $expected_route_name * The expected route name of the generated URL. * @param array $expected_route_parameters * The expected route parameters of the generated URL. * @param \Drupal\Tests\Core\Entity\UrlTestEntity|\PHPUnit\Framework\MockObject\MockObject $entity * The entity that is expected to be set as a URL option. * @param bool $has_language * Whether or not the URL is expected to have a language option. * @param \Drupal\Core\Url $url * The URL option to make the assertions on. * * @internal */

  protected function setUpEntityTypeDefinitions($definitions = []) {
    $class = get_class($this->createMock(EntityInterface::class));
    foreach ($definitions as $key => $entity_type) {
      // \Drupal\Core\Entity\EntityTypeInterface::getLinkTemplates() is called       // by \Drupal\Core\Entity\EntityTypeManager::processDefinition() so it must       // always be mocked.       $entity_type->getLinkTemplates()->willReturn([]);

      // Give the entity type a legitimate class to return.       $entity_type->getClass()->willReturn($class);
      $entity_type->setClass($class)->willReturn($entity_type->reveal());

      $definitions[$key] = $entity_type->reveal();
    }

    $this->discovery->getDefinition(Argument::cetera())
      ->will(function D$args) use ($definitions) {
        $entity_type_id = $args[0];
        

  protected function getContentEntityPaths() {
    if (!isset($this->contentEntityPaths)) {
      $this->contentEntityPaths = [];
      $entity_types = $this->entityTypeManager->getDefinitions();
      foreach ($entity_types as $entity_type_id => $entity_type) {
        if ($entity_type->entityClassImplements(ContentEntityInterface::class)) {
          $entity_paths = array_fill_keys($entity_type->getLinkTemplates()$entity_type_id);
          $this->contentEntityPaths = array_merge($this->contentEntityPaths, $entity_paths);
        }
      }
    }

    return $this->contentEntityPaths;
  }

}
$link_templates = $this->linkTemplates();
    return isset($link_templates[$rel]);
  }

  /** * Gets an array link templates. * * @return array * An array of link templates containing paths. */
  protected function linkTemplates() {
    return $this->getEntityType()->getLinkTemplates();
  }

  /** * {@inheritdoc} */
  public function toLink($text = NULL, $rel = 'canonical', array $options = []) {
    if (!isset($text)) {
      $text = $this->label();
    }
    $url = $this->toUrl($rel);
    $options += $url->getOptions();
    
$this->renderEntityFields($entity$this->display);
    $this->assertLink($value2, 0);
    $this->assertLinkByHref($entity->toUrl('revision')->toString());

    $this->renderEntityFields($entity_new_revision$this->display);
    $this->assertLink($value, 0);
    $this->assertLinkByHref('/entity_test_rev/' . $entity_new_revision->id() . '/revision/' . $entity_new_revision->getRevisionId() . '/view');

    // Check that linking to a revisionable entity works if the entity type does     // not specify a 'revision' link template.     $entity_type = clone $this->entityTypeManager->getDefinition('entity_test_rev');
    $link_templates = $entity_type->getLinkTemplates();
    unset($link_templates['revision']);
    $entity_type->set('links', $link_templates);
    \Drupal::state()->set('entity_test_rev.entity_type', $entity_type);
    $this->entityTypeManager->clearCachedDefinitions();

    $this->renderEntityFields($entity_new_revision$this->display);
    $this->assertLink($value, 0);
    $this->assertLinkByHref($entity->toUrl('canonical')->toString());
  }

  /** * Test "link_to_entity" feature on fields which are added to config entity. */
Home | Imprint | This part of the site doesn't use cookies.