uriRelationships example


class MediaLinkRelationsTest extends MediaKernelTestBase {

  /** * Tests that all link relationships for Media exist. */
  public function testExistLinkRelationships() {
    /** @var \Drupal\Core\Http\LinkRelationTypeManager $link_relation_type_manager */
    $link_relation_type_manager = $this->container->get('plugin.manager.link_relation_type');
    $media = Media::create(['bundle' => $this->testMediaType->id()]);
    $media->save();
    foreach ($media->uriRelationships() as $relation_name) {
      $this->assertTrue($link_relation_type_manager->hasDefinition($relation_name), "Link relationship '{$relation_name}' for a media item");
    }
  }

}
/** * {@inheritdoc} */
  public function mergeDefaultDisplaysOptions() {
    $this->storage->mergeDefaultDisplaysOptions();
  }

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

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

  /** * {@inheritdoc} */
$url_generator = $this->createMock(UrlGeneratorInterface::class);
    $container_builder->set('url_generator', $url_generator);
    \Drupal::setContainer($container_builder);

    // Test route with no mandatory parameters.     $this->registerLinkTemplate('canonical');
    $route_name_0 = 'entity.' . $this->entityTypeId . '.canonical';
    $url_generator->expects($this->any())
      ->method('generateFromRoute')
      ->with($route_name_0)
      ->willReturn((new GeneratedUrl())->setGeneratedUrl('/entity_test'));
    $this->assertEquals(['canonical']$entity->uriRelationships());

    // Test route with non-default mandatory parameters.     $this->registerLinkTemplate('{non_default_parameter}');
    $route_name_1 = 'entity.' . $this->entityTypeId . '.{non_default_parameter}';
    $url_generator->expects($this->any())
      ->method('generateFromRoute')
      ->with($route_name_1)
      ->willThrowException(new MissingMandatoryParametersException($route_name_1['missing_parameter']));
    $this->assertEquals([]$entity->uriRelationships());
  }

  
// Validate the received data before saving.     $this->validate($entity);
    try {
      $entity->save();
      $this->logger->notice('Created entity %type with ID %id.', ['%type' => $entity->getEntityTypeId(), '%id' => $entity->id()]);

      // 201 Created responses return the newly created entity in the response       // body. These responses are not cacheable, so we add no cacheability       // metadata here.       $headers = [];
      if (in_array('canonical', $entity->uriRelationships(), TRUE)) {
        $url = $entity->toUrl('canonical', ['absolute' => TRUE])->toString(TRUE);
        $headers['Location'] = $url->getGeneratedUrl();
      }
      return new ModifiedResourceResponse($entity, 201, $headers);
    }
    catch (EntityStorageException $e) {
      throw new HttpException(500, 'Internal Server Error', $e);
    }
  }

  /** * Responds to entity PATCH requests. * * @param \Drupal\Core\Entity\EntityInterface $original_entity * The original entity object. * @param \Drupal\Core\Entity\EntityInterface $entity * The entity. * * @return \Drupal\rest\ModifiedResourceResponse * The HTTP response object. * * @throws \Symfony\Component\HttpKernel\Exception\HttpException */
Home | Imprint | This part of the site doesn't use cookies.