本文整理汇总了PHP中Drupal\Core\Entity\EntityInterface::save方法的典型用法代码示例。如果您正苦于以下问题:PHP EntityInterface::save方法的具体用法?PHP EntityInterface::save怎么用?PHP EntityInterface::save使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Drupal\Core\Entity\EntityInterface
的用法示例。
在下文中一共展示了EntityInterface::save方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: setUp
protected function setUp()
{
parent::setUp();
// Use Classy theme for testing markup output.
\Drupal::service('theme_handler')->install(['classy']);
\Drupal::service('theme_handler')->setDefault('classy');
// Grant the 'view test entity' permission.
$this->installConfig(array('user'));
Role::load(RoleInterface::ANONYMOUS_ID)->grantPermission('view test entity')->save();
// The label formatter rendering generates links, so build the router.
$this->container->get('router.builder')->rebuild();
$this->createEntityReferenceField($this->entityType, $this->bundle, $this->fieldName, 'Field test', $this->entityType, 'default', array(), FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
// Set up a field, so that the entity that'll be referenced bubbles up a
// cache tag when rendering it entirely.
FieldStorageConfig::create(array('field_name' => 'body', 'entity_type' => $this->entityType, 'type' => 'text', 'settings' => array()))->save();
FieldConfig::create(['entity_type' => $this->entityType, 'bundle' => $this->bundle, 'field_name' => 'body', 'label' => 'Body'])->save();
entity_get_display($this->entityType, $this->bundle, 'default')->setComponent('body', array('type' => 'text_default', 'settings' => array()))->save();
FilterFormat::create(array('format' => 'full_html', 'name' => 'Full HTML'))->save();
// Create the entity to be referenced.
$this->referencedEntity = $this->container->get('entity_type.manager')->getStorage($this->entityType)->create(array('name' => $this->randomMachineName()));
$this->referencedEntity->body = array('value' => '<p>Hello, world!</p>', 'format' => 'full_html');
$this->referencedEntity->save();
// Create another entity to be referenced but do not save it.
$this->unsavedReferencedEntity = $this->container->get('entity_type.manager')->getStorage($this->entityType)->create(array('name' => $this->randomMachineName()));
$this->unsavedReferencedEntity->body = array('value' => '<p>Hello, unsaved world!</p>', 'format' => 'full_html');
}
示例2: testActionExecutionPostponed
/**
* Tests the action execution when saving is postponed.
*
* @covers ::execute
*/
public function testActionExecutionPostponed()
{
$this->entity->save()->shouldNotBeCalled();
$this->action->setContextValue('entity', $this->entity->reveal());
$this->action->execute();
$this->assertEquals($this->action->autoSaveContext(), ['entity'], 'Action returns the entity context name for auto saving.');
}
示例3: setUp
/**
* {@inheritdoc}
*/
protected function setUp()
{
parent::setUp();
$this->config = $this->config('rest.settings');
// Create an entity programmatically.
$this->entity = $this->entityCreate('entity_test');
$this->entity->save();
}
示例4: 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();
$form_state->setRedirectUrl($this->getCancelUrl());
}
示例5: setUp
/**
* {@inheritdoc}
*/
protected function setUp()
{
parent::setUp();
// Create an entity programmatic.
$this->entity = $this->entityCreate('entity_test');
$this->entity->save();
Role::load(AccountInterface::ANONYMOUS_ROLE)->grantPermission('view test entity')->save();
}
示例6: submitForm
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state)
{
// Remove the translated values.
$this->entity->removeTranslation($this->language->id);
$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->getSystemPath();
$conditions = array('source' => $path, 'langcode' => $this->language->id);
\Drupal::service('path.alias_storage')->delete($conditions);
}
$form_state->setRedirectUrl($this->getCancelUrl());
}
示例7: setUp
protected function setUp()
{
parent::setUp();
entity_reference_create_field($this->entityType, $this->bundle, $this->fieldName, 'Field test', $this->entityType);
// Set up a field, so that the entity that'll be referenced bubbles up a
// cache tag when rendering it entirely.
entity_create('field_storage_config', array('field_name' => 'body', 'entity_type' => $this->entityType, 'type' => 'text', 'settings' => array()))->save();
entity_create('field_config', array('entity_type' => $this->entityType, 'bundle' => $this->bundle, 'field_name' => 'body', 'label' => 'Body'))->save();
entity_get_display($this->entityType, $this->bundle, 'default')->setComponent('body', array('type' => 'text_default', 'settings' => array()))->save();
entity_create('filter_format', array('format' => 'full_html', 'name' => 'Full HTML'))->save();
// Create the entity to be referenced.
$this->referencedEntity = entity_create($this->entityType, array('name' => $this->randomMachineName()));
$this->referencedEntity->body = array('value' => '<p>Hello, world!</p>', 'format' => 'full_html');
$this->referencedEntity->save();
}
示例8: entitySaveReload
/**
* Saves and reloads an entity.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity to save.
*
* @return \Drupal\Core\Entity\EntityInterface
* The entity, freshly reloaded from storage.
*/
protected function entitySaveReload(EntityInterface $entity)
{
$entity->save();
$controller = $this->container->get('entity.manager')->getStorage($entity->getEntityTypeId());
$controller->resetCache();
return $controller->load($entity->id());
}
示例9: save
/**
* {@inheritdoc}
*/
public function save(array $form, FormStateInterface $form_state)
{
// Save as a new revision if requested to do so.
if (!$form_state->isValueEmpty('revision')) {
$this->entity->setNewRevision();
}
$insert = $this->entity->isNew();
$this->entity->save();
$context = ['@type' => $this->entity->bundle(), '%info' => $this->entity->label()];
$logger = $this->logger($this->entity->id());
$bundle_entity = $this->getBundleEntity();
$t_args = ['@type' => $bundle_entity ? $bundle_entity->label() : 'None', '%info' => $this->entity->label()];
if ($insert) {
$logger->notice('@type: added %info.', $context);
drupal_set_message($this->t('@type %info has been created.', $t_args));
} else {
$logger->notice('@type: updated %info.', $context);
drupal_set_message($this->t('@type %info has been updated.', $t_args));
}
if ($this->entity->id()) {
$form_state->setValue('id', $this->entity->id());
$form_state->set('id', $this->entity->id());
if ($this->entity->getEntityType()->hasLinkTemplate('collection')) {
$form_state->setRedirectUrl($this->entity->toUrl('collection'));
} else {
$form_state->setRedirectUrl($this->entity->toUrl('canonical'));
}
} else {
// In the unlikely case something went wrong on save, the entity will be
// rebuilt and entity form redisplayed.
drupal_set_message($this->t('The entity could not be saved.'), 'error');
$form_state->setRebuild();
}
}
示例10: cloneEntity
/**
* {@inheritdoc}
*/
public function cloneEntity(EntityInterface $entity, EntityInterface $cloned_entity, $properties = []) {
/** @var \Drupal\core\Config\Entity\ConfigEntityInterface $cloned_entity */
$id_key = $this->entityTypeManager->getDefinition($this->entityTypeId)->getKey('id');
$label_key = $this->entityTypeManager->getDefinition($this->entityTypeId)->getKey('label');
// Set new entity properties.
if (isset($properties['id'])) {
if ($id_key) {
$cloned_entity->set($id_key, $properties['id']);
}
unset($properties['id']);
}
if (isset($properties['label'])) {
if ($label_key) {
$cloned_entity->set($label_key, $properties['label']);
}
unset($properties['label']);
}
foreach ($properties as $key => $property) {
$cloned_entity->set($key, $property);
}
$cloned_entity->save();
return $cloned_entity;
}
示例11: cloneEntity
/**
* {@inheritdoc}
*/
public function cloneEntity(EntityInterface $entity, EntityInterface $cloned_entity, $properties = []) {
/** @var \Drupal\core\Entity\ContentEntityInterface $cloned_entity */
if ($label_key = $this->entityTypeManager->getDefinition($this->entityTypeId)->getKey('label')) {
$cloned_entity->set($label_key, $entity->label() . ' - Cloned');
}
$cloned_entity->save();
return $cloned_entity;
}
示例12: doExecute
/**
* Saves the Entity.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity to be saved.
* @param bool $immediate
* (optional) Save the entity immediately.
*/
protected function doExecute(EntityInterface $entity, $immediate)
{
// We only need to do something here if the immediate flag is set, otherwise
// the entity will be auto-saved after the execution.
if ((bool) $immediate) {
$entity->save();
$this->saveLater = FALSE;
}
}
示例13: 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);
}
示例14: testRegistrationTypeAPIDelete
/**
* Test registration type deletion.
*/
function testRegistrationTypeAPIDelete()
{
// Associate event with registration type.
$this->event->{EventManagerInterface::FIELD_REGISTRATION_TYPE}->appendItem(['target_id' => $this->registration_type->id()]);
$this->event->save();
$this->assertEqual(1, $this->countEventRegistrationTypeReferences($this->event->getEntityTypeId(), $this->registration_type->id()), 'One reference exists to this registration type');
$registration[0] = $this->createRegistration($this->event, $this->registration_type->id());
$this->registration_type->delete();
$this->assertIdentical(0, count(Registration::loadMultiple()), 'Registrations no longer exist');
$this->assertEqual(0, $this->countEventRegistrationTypeReferences($this->event->getEntityTypeId(), $this->registration_type->id()), 'No references from event entities to this registration type');
}
示例15: submitForm
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state)
{
// The revision timestamp will be updated when the revision is saved. Keep
// the original one for the confirmation message.
$this->revision = $this->prepareRevision($this->revision);
if ($this->revision instanceof RevisionLogInterface) {
$original_revision_timestamp = $this->revision->getRevisionCreationTime();
$this->revision->setRevisionLogMessage($this->t('Copy of the revision from %date.', ['%date' => $this->dateFormatter->format($original_revision_timestamp)]));
drupal_set_message(t('@type %title has been reverted to the revision from %revision-date.', ['@type' => $this->getBundleLabel($this->revision), '%title' => $this->revision->label(), '%revision-date' => $this->dateFormatter->format($original_revision_timestamp)]));
} else {
drupal_set_message(t('@type %title has been reverted', ['@type' => $this->getBundleLabel($this->revision), '%title' => $this->revision->label()]));
}
$this->revision->save();
$this->logger('content')->notice('@type: reverted %title revision %revision.', ['@type' => $this->revision->bundle(), '%title' => $this->revision->label(), '%revision' => $this->revision->getRevisionId()]);
$form_state->setRedirect("entity.{$this->revision->getEntityTypeId()}.version_history", [$this->revision->getEntityTypeId() => $this->revision->id()]);
}