本文整理汇总了PHP中Drupal\Core\Entity\ContentEntityInterface类的典型用法代码示例。如果您正苦于以下问题:PHP ContentEntityInterface类的具体用法?PHP ContentEntityInterface怎么用?PHP ContentEntityInterface使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了ContentEntityInterface类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: getRevisionDescription
protected function getRevisionDescription(ContentEntityInterface $revision, $is_current = FALSE)
{
/** @var \Drupal\Core\Entity\ContentEntityInterface|\Drupal\user\EntityOwnerInterface $revision */
if ($revision instanceof EntityOwnerInterface) {
$username = ['#theme' => 'username', '#account' => $revision->getOwner()];
} else {
$username = '';
}
if ($revision instanceof ExpandedEntityRevisionInterface) {
// Use revision link to link to revisions that are not active.
$date = $this->dateFormatter()->format($revision->getRevisionCreationTime(), 'short');
if (!$is_current) {
$link = $this->l($date, $revision->urlInfo('revision'));
} else {
$link = $revision->link($date);
}
} else {
$link = $revision->link($revision->label(), 'revision');
}
$markup = '';
if ($revision instanceof EntityRevisionLogInterface) {
$markup = $revision->getRevisionLog();
}
if ($username) {
$template = '{% trans %}{{ date }} by {{ username }}{% endtrans %}{% if message %}<p class="revision-log">{{ message }}</p>{% endif %}';
} else {
$template = '{% trans %} {{ date }} {% endtrans %}{% if message %}<p class="revision-log">{{ message }}</p>{% endif %}';
}
$column = ['data' => ['#type' => 'inline_template', '#template' => $template, '#context' => ['date' => $link, 'username' => $this->renderer()->renderPlain($username), 'message' => ['#markup' => $markup, '#allowed_tags' => Xss::getHtmlTagList()]]]];
return $column;
}
示例2: shouldModerate
/**
* Check if an entity's default revision and/or state needs adjusting.
*
* @param \Drupal\Core\Entity\ContentEntityInterface $entity
* The entity to check.
* @param bool $published_state
* Whether the state being transitioned to is a published state or not.
*
* @return bool
* TRUE when either the default revision or the state needs to be updated.
*/
protected function shouldModerate(ContentEntityInterface $entity, $published_state)
{
// @todo clarify the first condition.
// First condition is needed so you can add a translation.
// Second condition checks to see if the published status has changed.
return $entity->isDefaultTranslation() || $entity->isPublished() !== $published_state;
}
示例3: getEntityOwner
/**
* Get the entity owner if applicable.
*
* @param \Drupal\Core\Entity\ContentEntityInterface $entity
*
* @return \Drupal\user\UserInterface|null
*/
protected function getEntityOwner(ContentEntityInterface $entity)
{
if ($entity instanceof EntityOwnerInterface) {
return $entity->getOwner();
}
return NULL;
}
示例4: collectRenderDisplay
/**
* Returns the entity_form_display object used to build an entity form.
*
* Depending on the configuration of the form mode for the entity bundle, this
* can be either the display object associated to the form mode, or the
* 'default' display.
*
* This method should only be used internally when rendering an entity form.
* When assigning suggested display options for a component in a given form
* mode, entity_get_form_display() should be used instead, in order to avoid
* inadvertently modifying the output of other form modes that might happen to
* use the 'default' display too. Those options will then be effectively
* applied only if the form mode is configured to use them.
*
* hook_entity_form_display_alter() is invoked on each display, allowing 3rd
* party code to alter the display options held in the display before they are
* used to generate render arrays.
*
* @param \Drupal\Core\Entity\ContentEntityInterface $entity
* The entity for which the form is being built.
* @param string $form_mode
* The form mode.
*
* @return \Drupal\Core\Entity\Display\EntityFormDisplayInterface
* The display object that should be used to build the entity form.
*
* @see entity_get_form_display()
* @see hook_entity_form_display_alter()
*/
public static function collectRenderDisplay(ContentEntityInterface $entity, $form_mode)
{
$entity_type = $entity->getEntityTypeId();
$bundle = $entity->bundle();
// Check the existence and status of:
// - the display for the form mode,
// - the 'default' display.
if ($form_mode != 'default') {
$candidate_ids[] = $entity_type . '.' . $bundle . '.' . $form_mode;
}
$candidate_ids[] = $entity_type . '.' . $bundle . '.default';
$results = \Drupal::entityQuery('entity_form_display')->condition('id', $candidate_ids)->condition('status', TRUE)->execute();
// Load the first valid candidate display, if any.
$storage = \Drupal::entityManager()->getStorage('entity_form_display');
foreach ($candidate_ids as $candidate_id) {
if (isset($results[$candidate_id])) {
$display = $storage->load($candidate_id);
break;
}
}
// Else create a fresh runtime object.
if (empty($display)) {
$display = $storage->create(array('targetEntityType' => $entity_type, 'bundle' => $bundle, 'mode' => $form_mode, 'status' => TRUE));
}
// Let the display know which form mode was originally requested.
$display->originalMode = $form_mode;
// Let modules alter the display.
$display_context = array('entity_type' => $entity_type, 'bundle' => $bundle, 'form_mode' => $form_mode);
\Drupal::moduleHandler()->alter('entity_form_display', $display, $display_context);
return $display;
}
示例5: setEntity
/**
* Set the entity of this source.
*/
public function setEntity(ContentEntityInterface $entity)
{
$this->entity = $entity;
if ($this->entity->hasTranslation($this->getLanguage())) {
$this->entity = $this->entity->getTranslation($this->getLanguage());
}
}
示例6: onPresave
/**
* {@inheritdoc}
*/
public function onPresave(ContentEntityInterface $entity, $default_revision, $published_state)
{
// This is *probably* not necessary if configuration is setup correctly,
// but it can't hurt.
$entity->setNewRevision(TRUE);
// A newly-created revision is always the default revision, or else
// it gets lost.
$entity->isDefaultRevision($entity->isNew() || $default_revision);
}
示例7: 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);
}
示例8: 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;
}
示例9: 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);
}
示例10: assertPropertyPath
/**
* Asserts that a context for the given property path can be derived.
*
* @param \Drupal\Core\Entity\ContentEntityInterface $entity
* The entity to test with.
* @param $property_path
* The property path to look for.
* @param $expected_data_type
* The expected data type.
*
* @return \Drupal\Core\Plugin\Context\ContextInterface
* The context with a value.
*/
protected function assertPropertyPath(ContentEntityInterface $entity, $property_path, $expected_data_type)
{
$typed_data_entity = $entity->getTypedData();
$context_definition = new ContextDefinition($typed_data_entity->getDataDefinition()->getDataType());
$context_with_value = new Context($context_definition, $typed_data_entity);
$context_without_value = new Context($context_definition);
// Test the context without value.
$property_context = $this->typedDataResolver->getContextFromProperty($property_path, $context_without_value);
$this->assertEquals($expected_data_type, $property_context->getContextDefinition()->getDataType());
// Test the context with value.
$property_context = $this->typedDataResolver->getContextFromProperty($property_path, $context_with_value);
$this->assertEquals($expected_data_type, $property_context->getContextDefinition()->getDataType());
// Return the context with value so it can be asserted.
return $property_context;
}
示例11: overviewRow
/**
* Builds a table row for overview form.
*
* @param array ContentEntityInterface $entity
* Data needed to build the list row.
* @param array $bundles
* The array of bundles.
*
* @return array
*/
public function overviewRow(ContentEntityInterface $entity, array $bundles)
{
$label = $entity->label() ?: $this->t('@type: @id', array('@type' => $entity->getEntityTypeId(), '@id' => $entity->id()));
// Get existing translations and current job items for the entity
// to determine translation statuses
$translations = $entity->getTranslationLanguages();
$source_lang = $entity->language()->getId();
$current_job_items = tmgmt_job_item_load_latest('content', $entity->getEntityTypeId(), $entity->id(), $source_lang);
$row = array('id' => $entity->id(), 'title' => $entity->hasLinkTemplate('canonical') ? $entity->toLink($label, 'canonical')->toString() : ($entity->label() ?: $entity->id()));
if (count($bundles) > 1) {
$row['bundle'] = isset($bundles[$entity->bundle()]) ? $bundles[$entity->bundle()] : t('Unknown');
}
// Load entity translation specific data.
$manager = \Drupal::service('content_translation.manager');
foreach (\Drupal::languageManager()->getLanguages() as $langcode => $language) {
$translation_status = 'current';
if ($langcode == $source_lang) {
$translation_status = 'original';
} elseif (!isset($translations[$langcode])) {
$translation_status = 'missing';
} elseif ($translation = $entity->getTranslation($langcode)) {
$metadata = $manager->getTranslationMetadata($translation);
if ($metadata->isOutdated()) {
$translation_status = 'outofdate';
}
}
$build = $this->buildTranslationStatus($translation_status, isset($current_job_items[$langcode]) ? $current_job_items[$langcode] : NULL);
$row['langcode-' . $langcode] = ['data' => \Drupal::service('renderer')->render($build), 'class' => array('langstatus-' . $langcode)];
}
return $row;
}
示例12: 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();
}
示例13: prepareTranslation
/**
* Populates target values with the source values.
*
* @param \Drupal\Core\Entity\ContentEntityInterface $entity
* The entity being translated.
* @param \Drupal\Core\Language\LanguageInterface $source
* The language to be used as source.
* @param \Drupal\Core\Language\LanguageInterface $target
* The language to be used as target.
*/
public function prepareTranslation(ContentEntityInterface $entity, LanguageInterface $source, LanguageInterface $target)
{
/* @var \Drupal\Core\Entity\ContentEntityInterface $source_translation */
$source_translation = $entity->getTranslation($source->getId());
$target_translation = $entity->addTranslation($target->getId(), $source_translation->toArray());
// Make sure we do not inherit the affected status from the source values.
if ($entity->getEntityType()->isRevisionable()) {
$target_translation->setRevisionTranslationAffected(NULL);
}
/** @var \Drupal\user\UserInterface $user */
$user = $this->entityManager()->getStorage('user')->load($this->currentUser()->id());
$metadata = $this->manager->getTranslationMetadata($target_translation);
// Update the translation author to current user, as well the translation
// creation time.
$metadata->setAuthor($user);
$metadata->setCreatedTime(REQUEST_TIME);
}
示例14: parseEntity
/**
* Transforms an entity into an array of strings.
*
* Parses an entity's fields and for every field it builds an array of string
* to be compared. Basically this function transforms an entity into an array
* of strings.
*
* @param ContentEntityInterface $entity
* An entity containing fields.
*
* @return array
* Array of strings resulted by parsing the entity.
*/
public function parseEntity(ContentEntityInterface $entity)
{
$result = array();
$langcode = \Drupal::languageManager()->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)->getId();
// Load entity of current language, otherwise fields are always compared by
// their default language.
if ($entity->hasTranslation($langcode)) {
$entity = $entity->getTranslation($langcode);
}
$entity_type_id = $entity->getEntityTypeId();
// Load all entity base fields.
$entity_base_fields = $this->entityManager->getBaseFieldDefinitions($entity_type_id);
// Loop through entity fields and transform every FieldItemList object
// into an array of strings according to field type specific settings.
foreach ($entity as $field_items) {
$field_type = $field_items->getFieldDefinition()->getType();
$plugin_config = $this->pluginsConfig->get('field_types.' . $field_type);
$plugin = NULL;
if ($plugin_config && $plugin_config['type'] != 'hidden') {
$plugin = $this->diffBuilderManager->createInstance($plugin_config['type'], $plugin_config['settings']);
}
if ($plugin) {
// Configurable field. It is the responsibility of the class extending
// this class to hide some configurable fields from comparison. This
// class compares all configurable fields.
if (!array_key_exists($field_items->getName(), $entity_base_fields)) {
$build = $plugin->build($field_items);
if (!empty($build)) {
$result[$field_items->getName()] = $build;
}
} else {
// Check if this field needs to be compared.
$config_key = 'entity.' . $entity_type_id . '.' . $field_items->getName();
$enabled = $this->config->get($config_key);
if ($enabled) {
$build = $plugin->build($field_items);
if (!empty($build)) {
$result[$field_items->getName()] = $build;
}
}
}
}
}
return $result;
}
示例15: checkAccess
protected function checkAccess(ContentEntityInterface $entity, AccountInterface $account, $operation = 'view')
{
$entity_type_id = $entity->getEntityTypeId();
$entity_access = $this->entityManager->getAccessControlHandler($entity_type_id);
/** @var \Drupal\content_entity_base\Entity\Storage\RevisionableStorageInterface|\Drupal\Core\Entity\EntityStorageInterface $entity_storage */
$entity_storage = $this->entityManager->getStorage($entity_type_id);
if (!$entity_storage instanceof RevisionableStorageInterface) {
throw new \InvalidArgumentException('The entity storage has to implement \\Drupal\\content_entity_base\\Entity\\Storage\\RevisionableStorageInterface');
}
$map = ['view' => "view all {$entity_type_id} revisions", 'update' => "revert all {$entity_type_id} revisions", 'delete' => "delete all {$entity_type_id} revisions"];
$bundle = $entity->bundle();
$type_map = ['view' => "view {$entity_type_id} {$bundle} revisions", 'update' => "revert {$entity_type_id} {$bundle} revisions", 'delete' => "delete {$entity_type_id} {$bundle} revisions"];
if (!$entity || !isset($map[$operation]) || !isset($type_map[$operation])) {
// If there was no node to check against, or the $op was not one of the
// supported ones, we return access denied.
return FALSE;
}
// Statically cache access by revision ID, language code, user account ID,
// and operation.
$langcode = $entity->language()->getId();
$cid = $entity->getRevisionId() . ':' . $langcode . ':' . $account->id() . ':' . $operation;
if (!isset($this->access[$cid])) {
// Perform basic permission checks first.
if (!$account->hasPermission($map[$operation]) && !$account->hasPermission($type_map[$operation]) && !$account->hasPermission('administer nodes')) {
$this->access[$cid] = FALSE;
return FALSE;
}
// There should be at least two revisions. If the vid of the given node
// and the vid of the default revision differ, then we already have two
// different revisions so there is no need for a separate database check.
// Also, if you try to revert to or delete the default revision, that's
// not good.
if ($entity->isDefaultRevision() && ($entity_storage->countDefaultLanguageRevisions($entity) == 1 || $operation == 'update' || $operation == 'delete')) {
$this->access[$cid] = FALSE;
} elseif ($account->hasPermission('administer ' . $entity_type_id)) {
$this->access[$cid] = TRUE;
} else {
// First check the access to the default revision and finally, if the
// node passed in is not the default revision then access to that, too.
$this->access[$cid] = $entity_access->access($entity_storage->load($entity->id()), $operation, $account) && ($entity->isDefaultRevision() || $entity_access->access($entity, $operation, $account));
}
}
return $this->access[$cid];
}