本文整理汇总了PHP中Drupal\Core\Entity\EntityInterface::urlInfo方法的典型用法代码示例。如果您正苦于以下问题:PHP EntityInterface::urlInfo方法的具体用法?PHP EntityInterface::urlInfo怎么用?PHP EntityInterface::urlInfo使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Drupal\Core\Entity\EntityInterface
的用法示例。
在下文中一共展示了EntityInterface::urlInfo方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: testAuthentication
/**
* Tests that a resource without authentication cannot be enabled.
*/
public function testAuthentication()
{
$this->resourceConfigStorage->create(['id' => 'entity.entity_test', 'granularity' => RestResourceConfigInterface::METHOD_GRANULARITY, 'configuration' => ['GET' => ['supported_formats' => ['hal_json']]]])->save();
// Verify that accessing the resource returns 401.
$response = $this->httpRequest($this->entity->urlInfo()->setRouteParameter('_format', $this->defaultFormat), 'GET');
// \Drupal\Core\Routing\RequestFormatRouteFilter considers the canonical,
// non-REST route a match, but a lower quality one: no format restrictions
// means there's always a match and hence when there is no matching REST
// route, the non-REST route is used, but can't render into
// application/hal+json, so it returns a 406.
$this->assertResponse('406', 'HTTP response code is 406 when the resource does not define formats, because it falls back to the canonical, non-REST route.');
$this->curlClose();
}
示例2: submitForm
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state)
{
// Remove the translated values.
$this->entity = $this->entity->getUntranslated();
$this->entity->removeTranslation($this->language->getId());
$this->entity->save();
// Remove any existing path alias for the removed translation.
// @todo This should be taken care of by the Path module.
if (\Drupal::moduleHandler()->moduleExists('path')) {
$path = $this->entity->urlInfo()->getInternalPath();
$conditions = array('source' => $path, 'langcode' => $this->language->getId());
\Drupal::service('path.alias_storage')->delete($conditions);
}
$form_state->setRedirectUrl($this->getCancelUrl());
}
示例3: testLabelFormatter
/**
* Tests the label formatter.
*/
public function testLabelFormatter()
{
$formatter = 'entity_reference_label';
// The 'link' settings is TRUE by default.
$build = $this->buildRenderArray([$this->referencedEntity, $this->unsavedReferencedEntity], $formatter);
$expected_item_1 = array('#type' => 'link', '#title' => $this->referencedEntity->label(), '#url' => $this->referencedEntity->urlInfo(), '#options' => $this->referencedEntity->urlInfo()->getOptions(), '#cache' => array('tags' => $this->referencedEntity->getCacheTags()));
$this->assertEqual(drupal_render($build[0]), drupal_render($expected_item_1), sprintf('The markup returned by the %s formatter is correct for an item with a saved entity.', $formatter));
// The second referenced entity is "autocreated", therefore not saved and
// lacking any URL info.
$expected_item_2 = array('#markup' => $this->unsavedReferencedEntity->label(), '#cache' => array('tags' => $this->unsavedReferencedEntity->getCacheTags()));
$this->assertEqual($build[1], $expected_item_2, sprintf('The markup returned by the %s formatter is correct for an item with a unsaved entity.', $formatter));
// Test with the 'link' setting set to FALSE.
$build = $this->buildRenderArray([$this->referencedEntity, $this->unsavedReferencedEntity], $formatter, array('link' => FALSE));
$this->assertEqual($build[0]['#markup'], $this->referencedEntity->label(), sprintf('The markup returned by the %s formatter is correct for an item with a saved entity.', $formatter));
$this->assertEqual($build[1]['#markup'], $this->unsavedReferencedEntity->label(), sprintf('The markup returned by the %s formatter is correct for an item with a unsaved entity.', $formatter));
// Test an entity type that doesn't have any link templates, which means
// \Drupal\Core\Entity\EntityInterface::urlInfo() will throw an exception
// and the label formatter will output only the label instead of a link.
$field_storage_config = FieldStorageConfig::loadByName($this->entityType, $this->fieldName);
$field_storage_config->setSetting('target_type', 'entity_test_label');
$field_storage_config->save();
$referenced_entity_with_no_link_template = entity_create('entity_test_label', array('name' => $this->randomMachineName()));
$referenced_entity_with_no_link_template->save();
$build = $this->buildRenderArray([$referenced_entity_with_no_link_template], $formatter, array('link' => TRUE));
$this->assertEqual($build[0]['#markup'], $referenced_entity_with_no_link_template->label(), sprintf('The markup returned by the %s formatter is correct for an entity type with no valid link template.', $formatter));
}
示例4: testLabelFormatter
/**
* Tests the label formatter.
*/
public function testLabelFormatter()
{
/** @var \Drupal\Core\Render\RendererInterface $renderer */
$renderer = $this->container->get('renderer');
$formatter = 'entity_reference_label';
// The 'link' settings is TRUE by default.
$build = $this->buildRenderArray([$this->referencedEntity, $this->unsavedReferencedEntity], $formatter);
$expected_field_cacheability = ['contexts' => [], 'tags' => [], 'max-age' => Cache::PERMANENT];
$this->assertEqual($build['#cache'], $expected_field_cacheability, 'The field render array contains the entity access cacheability metadata');
$expected_item_1 = array('#type' => 'link', '#title' => $this->referencedEntity->label(), '#url' => $this->referencedEntity->urlInfo(), '#options' => $this->referencedEntity->urlInfo()->getOptions(), '#cache' => array('contexts' => ['user.permissions'], 'tags' => $this->referencedEntity->getCacheTags()));
$this->assertEqual($renderer->renderRoot($build[0]), $renderer->renderRoot($expected_item_1), sprintf('The markup returned by the %s formatter is correct for an item with a saved entity.', $formatter));
$this->assertEqual(CacheableMetadata::createFromRenderArray($build[0]), CacheableMetadata::createFromRenderArray($expected_item_1));
// The second referenced entity is "autocreated", therefore not saved and
// lacking any URL info.
$expected_item_2 = array('#plain_text' => $this->unsavedReferencedEntity->label(), '#cache' => array('contexts' => ['user.permissions'], 'tags' => $this->unsavedReferencedEntity->getCacheTags(), 'max-age' => Cache::PERMANENT));
$this->assertEqual($build[1], $expected_item_2, sprintf('The render array returned by the %s formatter is correct for an item with a unsaved entity.', $formatter));
// Test with the 'link' setting set to FALSE.
$build = $this->buildRenderArray([$this->referencedEntity, $this->unsavedReferencedEntity], $formatter, array('link' => FALSE));
$this->assertEqual($build[0]['#plain_text'], $this->referencedEntity->label(), sprintf('The markup returned by the %s formatter is correct for an item with a saved entity.', $formatter));
$this->assertEqual($build[1]['#plain_text'], $this->unsavedReferencedEntity->label(), sprintf('The markup returned by the %s formatter is correct for an item with a unsaved entity.', $formatter));
// Test an entity type that doesn't have any link templates, which means
// \Drupal\Core\Entity\EntityInterface::urlInfo() will throw an exception
// and the label formatter will output only the label instead of a link.
$field_storage_config = FieldStorageConfig::loadByName($this->entityType, $this->fieldName);
$field_storage_config->setSetting('target_type', 'entity_test_label');
$field_storage_config->save();
$referenced_entity_with_no_link_template = EntityTestLabel::create(array('name' => $this->randomMachineName()));
$referenced_entity_with_no_link_template->save();
$build = $this->buildRenderArray([$referenced_entity_with_no_link_template], $formatter, array('link' => TRUE));
$this->assertEqual($build[0]['#plain_text'], $referenced_entity_with_no_link_template->label(), sprintf('The markup returned by the %s formatter is correct for an entity type with no valid link template.', $formatter));
}
示例5: buildRow
/**
* {@inheritdoc}
*/
public function buildRow(EntityInterface $entity)
{
$row = array();
$row['date']['data'] = $entity->get('activity_date')->view(array('label' => 'hidden'));
$row['title']['data'] = array('#type' => 'link', '#title' => SafeMarkup::checkPlain($entity->label()), '#url' => $entity->urlInfo());
$row['type'] = $entity->get('type')->entity->label();
return $row + parent::buildRow($entity);
}
示例6: buildRow
/**
* {@inheritdoc}
*/
public function buildRow(EntityInterface $entity)
{
/* @var $entity \Drupal\commerce_store\Entity\Store */
$store_type = StoreType::load($entity->bundle());
$row['name']['data'] = ['#type' => 'link', '#title' => $entity->label()] + $entity->urlInfo()->toRenderArray();
$row['type'] = $store_type->label();
return $row + parent::buildRow($entity);
}
示例7: buildRow
/**
* {@inheritdoc}
*/
public function buildRow(EntityInterface $entity)
{
$row = array();
$row['label']['data'] = array('#type' => 'link', '#title' => SafeMarkup::checkPlain($entity->label()), '#url' => $entity->urlInfo());
$row['type'] = SafeMarkup::checkPlain($entity->get('type')->entity->label());
$row['changed'] = $this->dateFormatter->format($entity->get('changed')->value, 'short');
return $row + parent::buildRow($entity);
}
示例8: assertNoEntityAliasExists
public function assertNoEntityAliasExists(EntityInterface $entity, $alias = NULL)
{
$path = array('source' => '/' . $entity->urlInfo()->getInternalPath());
if (!empty($alias)) {
$path['alias'] = $alias;
}
$this->assertNoAliasExists($path);
}
示例9: testAuthentication
/**
* Tests that a resource without authentication cannot be enabled.
*/
public function testAuthentication()
{
$settings = array('entity:entity_test' => array('GET' => array('supported_formats' => array('hal_json'))));
// Attempt to enable the resource.
$this->config->set('resources', $settings);
$this->config->save();
$this->rebuildCache();
// Verify that accessing the resource returns 401.
$response = $this->httpRequest($this->entity->urlInfo()->setRouteParameter('_format', $this->defaultFormat), 'GET');
// \Drupal\Core\Routing\RequestFormatRouteFilter considers the canonical,
// non-REST route a match, but a lower quality one: no format restrictions
// means there's always a match and hence when there is no matching REST
// route, the non-REST route is used, but can't render into
// application/hal+json, so it returns a 406.
$this->assertResponse('406', 'HTTP response code is 406 when the resource does not define formats, because it falls back to the canonical, non-REST route.');
$this->curlClose();
}
示例10: renderLink
/**
* Prepares the link to the node.
*
* @param \Drupal\Core\Entity\EntityInterface $node
* The node entity this field belongs to.
* @param ResultRow $values
* The values retrieved from the view's result set.
*
* @return string
* Returns a string for the link text.
*/
protected function renderLink($node, ResultRow $values)
{
if ($node->access('view')) {
$this->options['alter']['make_link'] = TRUE;
$this->options['alter']['url'] = $node->urlInfo();
$text = !empty($this->options['text']) ? $this->options['text'] : $this->t('View');
return $text;
}
}
示例11: renderLink
/**
* {@inheritdoc}
*/
protected function renderLink(EntityInterface $entity, ResultRow $values)
{
if ($entity && $entity->access('update')) {
$this->options['alter']['make_link'] = TRUE;
$text = !empty($this->options['text']) ? $this->options['text'] : $this->t('Edit');
$this->options['alter']['url'] = $entity->urlInfo('edit-form', ['query' => ['destination' => $this->getDestinationArray()]]);
return $text;
}
}
示例12: buildRow
/**
* {@inheritdoc}
*/
public function buildRow(EntityInterface $entity)
{
/** @var \Drupal\commerce_product\Entity\ProductTypeInterface $product_type */
$product_type = ProductType::load($entity->bundle());
$row['title']['data'] = ['#type' => 'link', '#title' => $entity->label()] + $entity->urlInfo()->toRenderArray();
$row['type'] = $product_type->label();
$row['status'] = $entity->isPublished() ? $this->t('Published') : $this->t('Unpublished');
return $row + parent::buildRow($entity);
}
示例13: assertWorkflows
/**
* Checks that workflows have the expected behaviors for the given user.
*
* @param \Drupal\user\UserInterface $user
* The user to test the workflow behavior against.
* @param array $expected_status
* The an associative array with the operation name as key and the expected
* status as value.
*/
protected function assertWorkflows(UserInterface $user, $expected_status)
{
$default_langcode = $this->langcodes[0];
$languages = $this->container->get('language_manager')->getLanguages();
$args = array('@user_label' => $user->getUsername());
$this->drupalLogin($user);
// Check whether the user is allowed to access the entity form in edit mode.
$options = array('language' => $languages[$default_langcode], 'absolute' => TRUE);
$edit_url = $this->entity->urlInfo('edit-form', $options);
$this->drupalGet($edit_url, $options);
$this->assertResponse($expected_status['edit'], format_string('The @user_label has the expected edit access.', $args));
// Check whether the user is allowed to access the translation overview.
$langcode = $this->langcodes[1];
$options = array('language' => $languages[$langcode], 'absolute' => TRUE);
$translations_url = $this->entity->url('drupal:content-translation-overview', $options);
$this->drupalGet($translations_url);
$this->assertResponse($expected_status['overview'], format_string('The @user_label has the expected translation overview access.', $args));
// Check whether the user is allowed to create a translation.
$add_translation_url = Url::fromRoute('content_translation.translation_add_' . $this->entityTypeId, [$this->entityTypeId => $this->entity->id(), 'source' => $default_langcode, 'target' => $langcode], $options);
if ($expected_status['add_translation'] == 200) {
$this->clickLink('Add');
$this->assertUrl($add_translation_url->toString(), array(), 'The translation overview points to the translation form when creating translations.');
// Check that the translation form does not contain shared elements for
// translators.
if ($expected_status['edit'] == 403) {
$this->assertNoSharedElements();
}
} else {
$this->drupalGet($add_translation_url);
}
$this->assertResponse($expected_status['add_translation'], format_string('The @user_label has the expected translation creation access.', $args));
// Check whether the user is allowed to edit a translation.
$langcode = $this->langcodes[2];
$options['language'] = $languages[$langcode];
$edit_translation_url = Url::fromRoute('content_translation.translation_edit_' . $this->entityTypeId, [$this->entityTypeId => $this->entity->id(), 'language' => $langcode], $options);
$options = ['language' => $languages[$langcode], 'absolute' => TRUE];
if ($expected_status['edit_translation'] == 200) {
$this->drupalGet($translations_url);
$editor = $expected_status['edit'] == 200;
if ($editor) {
$this->clickLink('Edit', 2);
// An editor should be pointed to the entity form in multilingual mode.
// We need a new expected edit path with a new language.
$expected_edit_path = $this->entity->url('edit-form', $options);
$this->assertUrl($expected_edit_path, [], 'The translation overview points to the edit form for editors when editing translations.');
} else {
$this->clickLink('Edit');
// While a translator should be pointed to the translation form.
$this->assertUrl($edit_translation_url->toString(), array(), 'The translation overview points to the translation form for translators when editing translations.');
// Check that the translation form does not contain shared elements.
$this->assertNoSharedElements();
}
} else {
$this->drupalGet($edit_translation_url);
}
$this->assertResponse($expected_status['edit_translation'], format_string('The @user_label has the expected translation creation access.', $args));
}
示例14: doExecute
/**
* Creates entity path alias.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity that should get an alias.
* @param string $alias
* The alias to be created.
*/
protected function doExecute(EntityInterface $entity, $alias)
{
// We need to save the entity before we can get its internal path.
if ($entity->isNew()) {
$entity->save();
}
$path = $entity->urlInfo()->getInternalPath();
$langcode = $entity->language()->getId();
$this->aliasStorage->save($path, $alias, $langcode);
}
示例15: renderLink
/**
* {@inheritdoc}
*/
protected function renderLink(EntityInterface $entity, ResultRow $values)
{
if ($entity && $entity->access('delete')) {
$this->options['alter']['make_link'] = TRUE;
$text = !empty($this->options['text']) ? $this->options['text'] : $this->t('Cancel account');
$this->options['alter']['url'] = $entity->urlInfo('cancel-form');
$this->options['alter']['query'] = drupal_get_destination();
return $text;
}
}