本文整理汇总了PHP中Drupal\Core\Entity\ContentEntityInterface::id方法的典型用法代码示例。如果您正苦于以下问题:PHP ContentEntityInterface::id方法的具体用法?PHP ContentEntityInterface::id怎么用?PHP ContentEntityInterface::id使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Drupal\Core\Entity\ContentEntityInterface
的用法示例。
在下文中一共展示了ContentEntityInterface::id方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: testEntityUrlLanguageWithLanguageContentEnabled
/**
* Ensures correct entity URLs with the method language-content-entity enabled.
*
* Test case with the method language-content-entity enabled and configured
* with higher and also with lower priority than the method language-url.
*/
public function testEntityUrlLanguageWithLanguageContentEnabled()
{
// Define the method language-content-entity with a higher priority than
// language-url.
$config = $this->config('language.types');
$config->set('configurable', [LanguageInterface::TYPE_INTERFACE, LanguageInterface::TYPE_CONTENT]);
$config->set('negotiation.language_content.enabled', [LanguageNegotiationContentEntity::METHOD_ID => 0, LanguageNegotiationUrl::METHOD_ID => 1]);
$config->save();
// Without being on an content entity route the default entity URL tests
// should still pass.
$this->testEntityUrlLanguage();
// Now switching to an entity route, so that the URL links are generated
// while being on an entity route.
$this->setCurrentRequestForRoute('/entity_test/{entity_test}', 'entity.entity_test.canonical');
// The method language-content-entity should run before language-url and
// append query parameter for the content language and prevent language-url
// from overwriting the url.
$this->assertTrue(strpos($this->entity->urlInfo('canonical')->toString(), '/en/entity_test/' . $this->entity->id() . '?' . LanguageNegotiationContentEntity::QUERY_PARAMETER . '=en') !== FALSE);
$this->assertTrue(strpos($this->entity->getTranslation('es')->urlInfo('canonical')->toString(), '/en/entity_test/' . $this->entity->id() . '?' . LanguageNegotiationContentEntity::QUERY_PARAMETER . '=es') !== FALSE);
$this->assertTrue(strpos($this->entity->getTranslation('fr')->urlInfo('canonical')->toString(), '/en/entity_test/' . $this->entity->id() . '?' . LanguageNegotiationContentEntity::QUERY_PARAMETER . '=fr') !== FALSE);
// Define the method language-url with a higher priority than
// language-content-entity. This configuration should match the default one,
// where the language-content-entity is turned off.
$config->set('negotiation.language_content.enabled', [LanguageNegotiationUrl::METHOD_ID => 0, LanguageNegotiationContentEntity::METHOD_ID => 1]);
$config->save();
// The default entity URL tests should pass again with the current
// configuration.
$this->testEntityUrlLanguage();
}
示例2: testRedirects
/**
* Will test the redirects.
*/
public function testRedirects()
{
// Test alias normalization.
$this->config->set('normalize_aliases', TRUE)->save();
$this->assertRedirect('node/' . $this->node->id(), 'test-node');
$this->assertRedirect('Test-node', 'test-node');
$this->config->set('normalize_aliases', FALSE)->save();
$this->assertRedirect('node/' . $this->node->id(), NULL, 'HTTP/1.1 200 OK');
$this->assertRedirect('Test-node', NULL, 'HTTP/1.1 200 OK');
// Test deslashing.
$this->config->set('deslash', TRUE)->save();
$this->assertRedirect('test-node/', 'test-node');
$this->config->set('deslash', FALSE)->save();
$this->assertRedirect('test-node/', NULL, 'HTTP/1.1 200 OK');
// Test front page redirects.
$this->config->set('frontpage_redirect', TRUE)->save();
$this->config('system.site')->set('page.front', '/node')->save();
$this->assertRedirect('node', '<front>');
// Test front page redirects with an alias.
\Drupal::service('path.alias_storage')->save('/node', '/node-alias');
$this->assertRedirect('node-alias', '<front>');
$this->config->set('frontpage_redirect', FALSE)->save();
$this->assertRedirect('node', NULL, 'HTTP/1.1 200 OK');
$this->assertRedirect('node-alias', NULL, 'HTTP/1.1 200 OK');
// Test post request.
$this->config->set('normalize_aliases', TRUE)->save();
$this->drupalPost('Test-node', 'application/json', array());
// Does not do a redirect, stays in the same path.
$this->assertEqual(basename($this->getUrl()), 'Test-node');
// Test the access checking.
$this->config->set('normalize_aliases', TRUE)->save();
$this->config->set('access_check', TRUE)->save();
$this->assertRedirect('admin/config/system/site-information', NULL, 'HTTP/1.1 403 Forbidden');
$this->config->set('access_check', FALSE)->save();
// @todo - here it seems that the access check runs prior to our redirecting
// check why so and enable the test.
//$this->assertRedirect('admin/config/system/site-information', 'site-info');
// Login as user with admin privileges.
$this->drupalLogin($this->adminUser);
// Test ignoring admin paths.
$this->config->set('ignore_admin_path', FALSE)->save();
$this->assertRedirect('admin/config/system/site-information', 'site-info');
$this->config->set('ignore_admin_path', TRUE)->save();
$this->assertRedirect('admin/config/system/site-information', NULL, 'HTTP/1.1 200 OK');
}
示例3: doSaveFieldItems
/**
* {@inheritdoc}
*/
protected function doSaveFieldItems(ContentEntityInterface $entity, array $names = [])
{
// The anonymous user account is saved with the fixed user ID of 0.
// Therefore we need to check for NULL explicitly.
if ($entity->id() === NULL) {
$entity->uid->value = $this->database->nextId($this->database->query('SELECT MAX(uid) FROM {users}')->fetchField());
$entity->enforceIsNew();
}
return parent::doSaveFieldItems($entity, $names);
}
示例4: getAddLink
protected function getAddLink(ContentEntityInterface $host)
{
$link = '';
if ($host->access('update', \Drupal::currentUser())) {
$link = '<ul class="action-links action-links-field-collection-add"><li>';
$link .= \Drupal::l(t('Add'), Url::FromRoute('field_collection_item.add_page', ['field_collection' => $this->fieldDefinition->getName(), 'host_type' => $host->getEntityTypeId(), 'host_id' => $host->id()]));
$link .= '</li></ul>';
}
return $link;
}
示例5: synchronizeFields
/**
* {@inheritdoc}
*/
public function synchronizeFields(ContentEntityInterface $entity, $sync_langcode, $original_langcode = NULL)
{
$translations = $entity->getTranslationLanguages();
$field_type_manager = \Drupal::service('plugin.manager.field.field_type');
// If we have no information about what to sync to, if we are creating a new
// entity, if we have no translations for the current entity and we are not
// creating one, then there is nothing to synchronize.
if (empty($sync_langcode) || $entity->isNew() || count($translations) < 2) {
return;
}
// If the entity language is being changed there is nothing to synchronize.
$entity_type = $entity->getEntityTypeId();
$entity_unchanged = isset($entity->original) ? $entity->original : $this->entityManager->getStorage($entity_type)->loadUnchanged($entity->id());
if ($entity->getUntranslated()->language()->getId() != $entity_unchanged->getUntranslated()->language()->getId()) {
return;
}
/** @var \Drupal\Core\Field\FieldItemListInterface $items */
foreach ($entity as $field_name => $items) {
$field_definition = $items->getFieldDefinition();
$field_type_definition = $field_type_manager->getDefinition($field_definition->getType());
$column_groups = $field_type_definition['column_groups'];
// Sync if the field is translatable, not empty, and the synchronization
// setting is enabled.
if ($field_definition instanceof ThirdPartySettingsInterface && $field_definition->isTranslatable() && !$items->isEmpty() && ($translation_sync = $field_definition->getThirdPartySetting('content_translation', 'translation_sync'))) {
// Retrieve all the untranslatable column groups and merge them into
// single list.
$groups = array_keys(array_diff($translation_sync, array_filter($translation_sync)));
if (!empty($groups)) {
$columns = array();
foreach ($groups as $group) {
$info = $column_groups[$group];
// A missing 'columns' key indicates we have a single-column group.
$columns = array_merge($columns, isset($info['columns']) ? $info['columns'] : array($group));
}
if (!empty($columns)) {
$values = array();
foreach ($translations as $langcode => $language) {
$values[$langcode] = $entity->getTranslation($langcode)->get($field_name)->getValue();
}
// If a translation is being created, the original values should be
// used as the unchanged items. In fact there are no unchanged items
// to check against.
$langcode = $original_langcode ?: $sync_langcode;
$unchanged_items = $entity_unchanged->getTranslation($langcode)->get($field_name)->getValue();
$this->synchronizeItems($values, $unchanged_items, $sync_langcode, array_keys($translations), $columns);
foreach ($translations as $langcode => $language) {
$entity->getTranslation($langcode)->get($field_name)->setValue($values[$langcode]);
}
}
}
}
}
}
示例6: save
/**
* {@inheritdoc}
*/
protected function save(ContentEntityInterface $entity, array $old_destination_id_values = array())
{
// Do not overwrite the root account password.
if ($entity->id() != 1) {
// Set the pre_hashed password so that the PasswordItem field does not hash
// already hashed passwords. If the md5_passwords configuration option is
// set we need to rehash the password and prefix with a U.
// @see \Drupal\Core\Field\Plugin\Field\FieldType\PasswordItem::preSave()
$entity->pass->pre_hashed = TRUE;
if (isset($this->configuration['md5_passwords'])) {
$entity->pass->value = 'U' . $this->password->hash($entity->pass->value);
}
}
return parent::save($entity, $old_destination_id_values);
}
示例7: assertFieldStorageLangcode
/**
* Checks whether field languages are correctly stored for the given entity.
*
* @param \Drupal\Core\Entity\ContentEntityInterface $entity
* The entity fields are attached to.
* @param string $message
* (optional) A message to display with the assertion.
*/
protected function assertFieldStorageLangcode(ContentEntityInterface $entity, $message = '')
{
$status = TRUE;
$entity_type = $entity->getEntityTypeId();
$id = $entity->id();
$langcode = $entity->getUntranslated()->language()->id;
$fields = array($this->field_name, $this->untranslatable_field_name);
foreach ($fields as $field_name) {
$field_storage = FieldStorageConfig::loadByName($entity_type, $field_name);
$tables = array(ContentEntityDatabaseStorage::_fieldTableName($field_storage), ContentEntityDatabaseStorage::_fieldRevisionTableName($field_storage));
foreach ($tables as $table) {
$record = \Drupal::database()->select($table, 'f')->fields('f')->condition('f.entity_id', $id)->condition('f.revision_id', $id)->execute()->fetchObject();
if ($record->langcode != $langcode) {
$status = FALSE;
break;
}
}
}
return $this->assertTrue($status, $message);
}
示例8: getEntityId
/**
* Getter for the entity id.
*
* @return int mixed
* The entity id.
*/
public function getEntityId() {
return $this->entity->id();
}
示例9: sortById
protected function sortById(ContentEntityInterface $leftEntity, ContentEntityInterface $rightEntity)
{
$leftId = (int) $leftEntity->id();
$rightId = (int) $rightEntity->id();
return $leftId < $rightId ? -1 : 1;
}
示例10: hasContentBlocks
/**
* Check if the supplied entity layout has any content blocks.
*
* @param EntityLayoutInterface $entity_layout
* The entity layout to check content blocks for.
*
* @return bool
*/
public function hasContentBlocks(EntityLayoutInterface $entity_layout, ContentEntityInterface $entity)
{
return (bool) $this->entityTypeManager->getStorage('entity_layout_block')->getQuery()->condition('entity_id', $entity->id())->condition('layout', $entity_layout->id())->count()->execute();
}
示例11: create
/**
* {@inheritdoc}
*/
public function create(ContentEntityInterface $entity, $fields)
{
$query = $this->database->insert('comment_entity_statistics')->fields(array('entity_id', 'entity_type', 'field_name', 'cid', 'last_comment_timestamp', 'last_comment_name', 'last_comment_uid', 'comment_count'));
foreach ($fields as $field_name => $detail) {
// Skip fields that entity does not have.
if (!$entity->hasField($field_name)) {
continue;
}
// Get the user ID from the entity if it's set, or default to the
// currently logged in user.
$last_comment_uid = 0;
if ($entity instanceof EntityOwnerInterface) {
$last_comment_uid = $entity->getOwnerId();
}
if (!isset($last_comment_uid)) {
// Default to current user when entity does not implement
// EntityOwnerInterface or author is not set.
$last_comment_uid = $this->currentUser->id();
}
// Default to REQUEST_TIME when entity does not have a changed property.
$last_comment_timestamp = REQUEST_TIME;
if ($entity instanceof EntityChangedInterface) {
$last_comment_timestamp = $entity->getChangedTime();
}
$query->values(array('entity_id' => $entity->id(), 'entity_type' => $entity->getEntityTypeId(), 'field_name' => $field_name, 'cid' => 0, 'last_comment_timestamp' => $last_comment_timestamp, 'last_comment_name' => NULL, 'last_comment_uid' => $last_comment_uid, 'comment_count' => 0));
}
$query->execute();
}
示例12: buildRecord
/**
* @param \Drupal\Core\Entity\ContentEntityInterface $entity
* @return array
*/
protected function buildRecord(ContentEntityInterface $entity)
{
return array('entity_type_id' => $entity->getEntityTypeId(), 'entity_id' => $entity->id(), 'entity_uuid' => $entity->uuid(), 'revision_id' => $entity->getRevisionId(), 'deleted' => $entity->_deleted->value, 'rev' => $entity->_rev->value, 'seq' => $this->multiversionManager->newSequenceId(), 'local' => (bool) $entity->getEntityType()->get('local'), 'is_stub' => (bool) $entity->_rev->is_stub);
}
示例13: checkEntityValue
/**
* Check an entity value after reload.
*
* @param $entity_type_id
* @param \Drupal\Core\Entity\ContentEntityInterface $entity
* @param $field
* @param $value
*/
protected function checkEntityValue($entity_type_id, ContentEntityInterface $entity, $field, $value) {
$storage = \Drupal::entityTypeManager()->getStorage($entity_type_id);
$storage->resetCache([$entity->id()]);
$updated_entity = $storage->load($entity->id());
$this->assertEqual($updated_entity->get($field)->value, 1, $entity->label() . " $field = $value");
}
示例14: withinAncestry
/**
* Detect whether an entity is already reference within an ancestry ER array.
*
* @var \Drupal\Core\Entity\ContentEntityInterface $discussion
* A discussion node entity.
* @var array $ancestry
* A discussion node entity, coming from field_ancestry->getValue().
*/
protected function withinAncestry(ContentEntityInterface $entity, $ancestry)
{
$detected = FALSE;
foreach ($ancestry as $ancestor) {
if ($entity->id() == $ancestor['target_id']) {
$detected = TRUE;
}
}
return $detected;
}
示例15: countDefaultLanguageRevisions
/**
* Counts the number of revisions in the default language.
*
* @param \Drupal\Core\Entity\ContentEntityInterface $entity
* The entity.
* @param \Drupal\Core\Entity\EntityStorageInterface $entity_storage
* The entity storage.
*
* @return int
* The number of revisions in the default language.
*/
protected function countDefaultLanguageRevisions(ContentEntityInterface $entity, EntityStorageInterface $entity_storage)
{
$entity_type = $entity->getEntityType();
$count = $entity_storage->getQuery()->allRevisions()->condition($entity_type->getKey('id'), $entity->id())->condition($entity_type->getKey('default_langcode'), 1)->count()->execute();
return $count;
}