本文整理匯總了PHP中Drupal\Core\Entity\ContentEntityBase::preSave方法的典型用法代碼示例。如果您正苦於以下問題:PHP ContentEntityBase::preSave方法的具體用法?PHP ContentEntityBase::preSave怎麽用?PHP ContentEntityBase::preSave使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類Drupal\Core\Entity\ContentEntityBase
的用法示例。
在下文中一共展示了ContentEntityBase::preSave方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。
示例1: preSave
/**
* {@inheritdoc}
*/
public function preSave(EntityStorageInterface $storage)
{
parent::preSave($storage);
// Update the user password if it has changed.
if ($this->isNew() || $this->pass->value && $this->pass->value != $this->original->pass->value) {
// Allow alternate password hashing schemes.
$this->pass->value = \Drupal::service('password')->hash(trim($this->pass->value));
// Abort if the hashing failed and returned FALSE.
if (!$this->pass->value) {
throw new EntityMalformedException('The entity does not have a password.');
}
}
if (!$this->isNew()) {
// If the password is empty, that means it was not changed, so use the
// original password.
if (empty($this->pass->value)) {
$this->pass->value = $this->original->pass->value;
}
}
// Store account cancellation information.
foreach (array('user_cancel_method', 'user_cancel_notify') as $key) {
if (isset($this->{$key})) {
\Drupal::service('user.data')->set('user', $this->id(), substr($key, 5), $this->{$key});
}
}
}
示例2: preSave
/**
* {@inheritdoc}
*/
public function preSave(EntityStorageInterface $storage)
{
parent::preSave($storage);
// Make sure that the authenticated/anonymous roles are not persisted.
foreach ($this->get('roles') as $index => $item) {
if (in_array($item->target_id, array(RoleInterface::ANONYMOUS_ID, RoleInterface::AUTHENTICATED_ID))) {
$this->get('roles')->offsetUnset($index);
}
}
// Update the user password if it has changed.
if ($this->isNew() || $this->pass->value && $this->pass->value != $this->original->pass->value) {
// Allow alternate password hashing schemes.
$this->pass->value = \Drupal::service('password')->hash(trim($this->pass->value));
// Abort if the hashing failed and returned FALSE.
if (!$this->pass->value) {
throw new EntityMalformedException('The entity does not have a password.');
}
}
if (!$this->isNew()) {
// If the password is empty, that means it was not changed, so use the
// original password.
if (empty($this->pass->value)) {
$this->pass->value = $this->original->pass->value;
}
}
// Store account cancellation information.
foreach (array('user_cancel_method', 'user_cancel_notify') as $key) {
if (isset($this->{$key})) {
\Drupal::service('user.data')->set('user', $this->id(), substr($key, 5), $this->{$key});
}
}
}
示例3: preSave
/**
* {@inheritdoc}
*/
public function preSave(EntityStorageInterface $storage)
{
parent::preSave($storage);
// If no owner has been set explicitly, make the current user the owner.
if (!$this->getOwner()) {
$this->setOwnerId($this->getCurrentUserId());
}
}
示例4: preSave
/**
* {@inheritdoc}
*/
public function preSave(EntityStorageInterface $storage)
{
parent::preSave($storage);
// If no owner has been set explicitly, make the current user the owner.
if (!$this->getOwner()) {
$this->setOwnerId(\Drupal::currentUser()->id());
}
// If no revision author has been set explicitly, make the node owner the
// revision author.
if (!$this->getRevisionAuthor()) {
$this->setRevisionAuthorId($this->getOwnerId());
}
}
示例5: preSave
/**
* {@inheritdoc}
*/
public function preSave(EntityStorageInterface $storage)
{
parent::preSave($storage);
// If no owner has been set explicitly, make the current user the owner.
if (!$this->getOwner()) {
$this->setOwnerId(\Drupal::currentUser()->id());
}
if ($this->isNew()) {
if (!$this->getIpAddress()) {
$this->setIpAddress(\Drupal::request()->getClientIp());
}
if (!$this->getEmail()) {
$this->setEmail($this->getOwner()->getEmail());
}
}
}
示例6: preSave
/**
* {@inheritdoc}
*/
public function preSave(EntityStorageInterface $storage)
{
parent::preSave($storage);
// Make sure that the authenticated/anonymous roles are not persisted.
foreach ($this->get('roles') as $index => $item) {
if (in_array($item->target_id, array(RoleInterface::ANONYMOUS_ID, RoleInterface::AUTHENTICATED_ID))) {
$this->get('roles')->offsetUnset($index);
}
}
// Store account cancellation information.
foreach (array('user_cancel_method', 'user_cancel_notify') as $key) {
if (isset($this->{$key})) {
\Drupal::service('user.data')->set('user', $this->id(), substr($key, 5), $this->{$key});
}
}
}
示例7: preSave
/**
* {@inheritdoc}
*/
public function preSave(EntityStorageInterface $storage) {
parent::preSave($storage);
/** @var \Drupal\entityqueue\EntityQueueInterface $queue */
$queue = $this->getQueue();
$max_size = $queue->getMaximumSize();
$act_as_queue = $queue->getActAsQueue();
$items = $this->get('items')->getValue();
$number_of_items = count($items);
// Remove extra items from the front of the queue if the maximum size is
// exceeded.
if ($act_as_queue && $number_of_items > $max_size) {
$items = array_slice($items, -$max_size);
$this->set('items', $items);
}
}
示例8: preSave
/**
* {@inheritdoc}
*/
public function preSave(EntityStorageInterface $storage)
{
parent::preSave($storage);
// If no owner has been set explicitly, make the current user the owner.
if (!$this->getOwner()) {
$this->setOwnerId(\Drupal::currentUser()->id());
}
if ($this->isNew()) {
if (!$this->getIpAddress()) {
$this->setIpAddress(\Drupal::request()->getClientIp());
}
if (!$this->getEmail()) {
$this->setEmail($this->getOwner()->getEmail());
}
}
// Recalculate the total.
// @todo Rework this once pricing is finished.
$this->total_price->amount = 0;
foreach ($this->getLineItems() as $line_item) {
$this->total_price->amount += $line_item->total_price->amount;
$this->total_price->currency_code = $line_item->total_price->currency_code;
}
}
示例9: preSave
public function preSave(EntityStorageInterface $storage)
{
parent::preSave($storage);
// TODO: Change the autogenerated stub
}
示例10: preSave
/**
* {@inheritdoc}
*/
public function preSave(EntityStorageInterface $storage)
{
parent::preSave($storage);
if (parse_url($this->link->uri, PHP_URL_SCHEME) === 'internal') {
$this->setRequiresRediscovery(TRUE);
} else {
$this->setRequiresRediscovery(FALSE);
}
}
示例11: preSave
/**
* {@inheritdoc}
*/
public function preSave(EntityStorageInterface $storage)
{
parent::preSave($storage);
foreach (array_keys($this->getTranslationLanguages()) as $langcode) {
$translation = $this->getTranslation($langcode);
// If no owner has been set explicitly, make the anonymous user the owner.
if (!$translation->getOwner()) {
$translation->setOwnerId(0);
}
}
// If no revision author has been set explicitly, make the node owner the
// revision author.
if (!$this->getRevisionAuthor()) {
$this->setRevisionAuthorId($this->getOwnerId());
}
}
示例12: preSave
/**
* {@inheritdoc}
*/
public function preSave(EntityStorageInterface $storage)
{
parent::preSave($storage);
$url = Url::createFromPath($this->path->value);
$this->setRouteName($url->getRouteName());
$this->setRouteParams($url->getRouteParameters());
}
示例13: preSave
/**
* {@inheritdoc}
*/
public function preSave(EntityStorageInterface $storage)
{
parent::preSave($storage);
// The file itself might not exist or be available right now.
$uri = $this->getFileUri();
if ($size = @filesize($uri)) {
$this->setSize($size);
}
}
示例14: preSave
/**
* {@inheritdoc}
*/
public function preSave(EntityStorageInterface $storage)
{
parent::preSave($storage);
$this->order_total->value = $this->getTotal();
$this->product_count->value = $this->getProductCount();
$this->host->value = \Drupal::request()->getClientIp();
$this->setChangedTime(REQUEST_TIME);
}
示例15: preSave
/**
* {@inheritdoc}
*/
public function preSave(EntityStorageInterface $storage)
{
parent::preSave($storage);
if (is_null($this->get('status')->value)) {
$published = \Drupal::currentUser()->hasPermission('skip comment approval') ? CommentInterface::PUBLISHED : CommentInterface::NOT_PUBLISHED;
$this->setPublished($published);
}
if ($this->isNew()) {
// Add the comment to database. This next section builds the thread field.
// Also see the documentation for comment_view().
$thread = $this->getThread();
if (empty($thread)) {
if ($this->threadLock) {
// Thread lock was not released after being set previously.
// This suggests there's a bug in code using this class.
throw new \LogicException('preSave() is called again without calling postSave() or releaseThreadLock()');
}
if (!$this->hasParentComment()) {
// This is a comment with no parent comment (depth 0): we start
// by retrieving the maximum thread level.
$max = $storage->getMaxThread($this);
// Strip the "/" from the end of the thread.
$max = rtrim($max, '/');
// We need to get the value at the correct depth.
$parts = explode('.', $max);
$n = Number::alphadecimalToInt($parts[0]);
$prefix = '';
} else {
// This is a comment with a parent comment, so increase the part of
// the thread value at the proper depth.
// Get the parent comment:
$parent = $this->getParentComment();
// Strip the "/" from the end of the parent thread.
$parent->setThread((string) rtrim((string) $parent->getThread(), '/'));
$prefix = $parent->getThread() . '.';
// Get the max value in *this* thread.
$max = $storage->getMaxThreadPerThread($this);
if ($max == '') {
// First child of this parent. As the other two cases do an
// increment of the thread number before creating the thread
// string set this to -1 so it requires an increment too.
$n = -1;
} else {
// Strip the "/" at the end of the thread.
$max = rtrim($max, '/');
// Get the value at the correct depth.
$parts = explode('.', $max);
$parent_depth = count(explode('.', $parent->getThread()));
$n = Number::alphadecimalToInt($parts[$parent_depth]);
}
}
// Finally, build the thread field for this new comment. To avoid
// race conditions, get a lock on the thread. If another process already
// has the lock, just move to the next integer.
do {
$thread = $prefix . Number::intToAlphadecimal(++$n) . '/';
$lock_name = "comment:{$this->getCommentedEntityId()}:{$thread}";
} while (!\Drupal::lock()->acquire($lock_name));
$this->threadLock = $lock_name;
}
// We test the value with '===' because we need to modify anonymous
// users as well.
if ($this->getOwnerId() === \Drupal::currentUser()->id() && \Drupal::currentUser()->isAuthenticated()) {
$this->setAuthorName(\Drupal::currentUser()->getUsername());
}
// Add the values which aren't passed into the function.
$this->setThread($thread);
$this->setHostname(\Drupal::request()->getClientIP());
}
}