本文整理汇总了PHP中Drupal\Core\Entity\ContentEntityForm::buildEntity方法的典型用法代码示例。如果您正苦于以下问题:PHP ContentEntityForm::buildEntity方法的具体用法?PHP ContentEntityForm::buildEntity怎么用?PHP ContentEntityForm::buildEntity使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Drupal\Core\Entity\ContentEntityForm
的用法示例。
在下文中一共展示了ContentEntityForm::buildEntity方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: buildEntity
/**
* {@inheritdoc}
*/
public function buildEntity(array $form, FormStateInterface $form_state) {
$entity = parent::buildEntity($form, $form_state);
if ($entity->isNew()) {
$entity->setCreatedTime(REQUEST_TIME);
}
return $entity;
}
示例2: buildEntity
/**
* {@inheritdoc}
*/
public function buildEntity(array $form, FormStateInterface $form_state)
{
$entity = parent::buildEntity($form, $form_state);
// Set the computed 'path' value so it can used in the preSave() method to
// derive the route name and parameters.
$entity->path->value = $form_state['values']['path'];
return $entity;
}
示例3: buildEntity
/**
* {@inheritdoc}
*/
public function buildEntity(array $form, FormStateInterface $form_state)
{
$term = parent::buildEntity($form, $form_state);
// Prevent leading and trailing spaces in term names.
$term->setName(trim($term->getName()));
// Assign parents with proper delta values starting from 0.
$term->parent = array_keys($form_state->getValue('parent'));
return $term;
}
示例4: buildEntity
/**
* {@inheritdoc}
*/
public function buildEntity(array $form, FormStateInterface $form_state)
{
/** @var \Drupal\comment\CommentInterface $comment */
$comment = parent::buildEntity($form, $form_state);
if (!$form_state->isValueEmpty('date') && $form_state->getValue('date') instanceof DrupalDateTime) {
$comment->setCreatedTime($form_state->getValue('date')->getTimestamp());
} else {
$comment->setCreatedTime(REQUEST_TIME);
}
// Empty author ID should revert to anonymous.
$author_id = $form_state->getValue('uid');
if ($comment->id() && $this->currentUser->hasPermission('administer comments')) {
// Admin can leave the author ID blank to revert to anonymous.
$author_id = $author_id ?: 0;
}
if (!is_null($author_id)) {
if ($author_id === 0 && $form['author']['name']['#access']) {
// Use the author name value when the form has access to the element and
// the author ID is anonymous.
$comment->setAuthorName($form_state->getValue('name'));
} else {
// Ensure the author name is not set.
$comment->setAuthorName(NULL);
}
} else {
$author_id = $this->currentUser->id();
}
$comment->setOwnerId($author_id);
// Validate the comment's subject. If not specified, extract from comment
// body.
if (trim($comment->getSubject()) == '') {
if ($comment->hasField('comment_body')) {
// The body may be in any format, so:
// 1) Filter it into HTML
// 2) Strip out all HTML tags
// 3) Convert entities back to plain-text.
$comment_text = $comment->comment_body->processed;
$comment->setSubject(Unicode::truncate(trim(Html::decodeEntities(strip_tags($comment_text))), 29, TRUE, TRUE));
}
// Edge cases where the comment body is populated only by HTML tags will
// require a default subject.
if ($comment->getSubject() == '') {
$comment->setSubject($this->t('(No subject)'));
}
}
return $comment;
}
示例5: buildEntity
/**
* {@inheritdoc}
*/
public function buildEntity(array $form, FormStateInterface $form_state)
{
/** @var \Drupal\commerce_order\Entity\LineItemInterface $entity */
$entity = parent::buildEntity($form, $form_state);
// Now that the purchased entity is set, populate the title and price.
$entity->setTitle($entity->getPurchasedEntity()->getLineItemTitle());
// @todo Remove once the price calculation is in place.
$entity->unit_price = $entity->getPurchasedEntity()->price;
return $entity;
}
示例6: buildEntity
/**
* {@inheritdoc}
*/
public function buildEntity(array $form, FormStateInterface $form_state)
{
// Change the roles array to a list of enabled roles.
// @todo: Alter the form state as the form values are directly extracted and
// set on the field, which throws an exception as the list requires
// numeric keys. Allow to override this per field. As this function is
// called twice, we have to prevent it from getting the array keys twice.
if (is_string(key($form_state->getValue('roles')))) {
$form_state->setValue('roles', array_keys(array_filter($form_state->getValue('roles'))));
}
/** @var \Drupal\user\UserInterface $account */
$account = parent::buildEntity($form, $form_state);
// Translate the empty value '' of language selects to an unset field.
foreach (array('preferred_langcode', 'preferred_admin_langcode') as $field_name) {
if ($form_state->getValue($field_name) === '') {
$account->{$field_name} = NULL;
}
}
// Set existing password if set in the form state.
$current_pass = trim($form_state->getValue('current_pass'));
if (strlen($current_pass) > 0) {
$account->setExistingPassword($current_pass);
}
// Skip the protected user field constraint if the user came from the
// password recovery page.
$account->_skipProtectedUserFieldConstraint = $form_state->get('user_pass_reset');
return $account;
}
示例7: buildEntity
/**
* {@inheritdoc}
*/
public function buildEntity(array $form, FormStateInterface $form_state)
{
/** @var \Drupal\menu_link_content\MenuLinkContentInterface $entity */
$entity = parent::buildEntity($form, $form_state);
$new_definition = $this->extractFormValues($form, $form_state);
$entity->parent->value = $new_definition['parent'];
$entity->menu_name->value = $new_definition['menu_name'];
$entity->hidden->value = (bool) $new_definition['hidden'];
$entity->expanded->value = $new_definition['expanded'];
$entity->url->value = $new_definition['url'];
$entity->route_name->value = $new_definition['route_name'];
$entity->setRouteParameters($new_definition['route_parameters']);
$entity->setOptions($new_definition['options']);
return $entity;
}
示例8: buildEntity
/**
* {@inheritdoc}
*/
public function buildEntity(array $form, FormStateInterface $form_state)
{
// Change the roles array to a list of enabled roles.
// @todo: Alter the form state as the form values are directly extracted and
// set on the field, which throws an exception as the list requires
// numeric keys. Allow to override this per field. As this function is
// called twice, we have to prevent it from getting the array keys twice.
if (is_string(key($form_state['values']['roles']))) {
$form_state['values']['roles'] = array_keys(array_filter($form_state['values']['roles']));
}
return parent::buildEntity($form, $form_state);
}
示例9: buildEntity
/**
* {@inheritdoc}
*/
public function buildEntity(array $form, FormStateInterface $form_state)
{
/** @var \Drupal\node\NodeInterface $entity */
$entity = parent::buildEntity($form, $form_state);
// A user might assign the node author by entering a user name in the node
// form, which we then need to translate to a user ID.
// @todo: Remove it when https://www.drupal.org/node/2322525 is pushed.
if (!empty($form_state->getValue('uid')[0]['target_id']) && ($account = User::load($form_state->getValue('uid')[0]['target_id']))) {
$entity->setOwnerId($account->id());
} else {
$entity->setOwnerId(0);
}
return $entity;
}
示例10: buildEntity
/**
* Overrides EntityForm::buildEntity().
*/
public function buildEntity(array $form, FormStateInterface $form_state)
{
$comment = parent::buildEntity($form, $form_state);
if (!$form_state->isValueEmpty('date') && $form_state->getValue('date') instanceof DrupalDateTime) {
$comment->setCreatedTime($form_state->getValue('date')->getTimestamp());
} else {
$comment->setCreatedTime(REQUEST_TIME);
}
$comment->changed->value = REQUEST_TIME;
return $comment;
}
示例11: buildEntity
/**
* {@inheritdoc}
*/
public function buildEntity(array $form, FormStateInterface $form_state)
{
/** @var \Drupal\comment\CommentInterface $comment */
$comment = parent::buildEntity($form, $form_state);
if (!$form_state->isValueEmpty('date') && $form_state->getValue('date') instanceof DrupalDateTime) {
$comment->setCreatedTime($form_state->getValue('date')->getTimestamp());
} else {
$comment->setCreatedTime(REQUEST_TIME);
}
$author_name = $form_state->getValue('name');
if (!$this->currentUser->isAnonymous()) {
// Assign the owner based on the given user name - none means anonymous.
$accounts = $this->entityManager->getStorage('user')->loadByProperties(array('name' => $author_name));
$account = reset($accounts);
$uid = $account ? $account->id() : 0;
$comment->setOwnerId($uid);
}
// If the comment was posted by an anonymous user and no author name was
// required, use "Anonymous" by default.
if ($comment->getOwnerId() === 0 && (!isset($author_name) || $author_name === '')) {
$comment->setAuthorName($this->config('user.settings')->get('anonymous'));
}
// Validate the comment's subject. If not specified, extract from comment
// body.
if (trim($comment->getSubject()) == '') {
if ($comment->hasField('comment_body')) {
// The body may be in any format, so:
// 1) Filter it into HTML
// 2) Strip out all HTML tags
// 3) Convert entities back to plain-text.
$comment_text = $comment->comment_body->processed;
$comment->setSubject(Unicode::truncate(trim(Html::decodeEntities(strip_tags($comment_text))), 29, TRUE, TRUE));
}
// Edge cases where the comment body is populated only by HTML tags will
// require a default subject.
if ($comment->getSubject() == '') {
$comment->setSubject($this->t('(No subject)'));
}
}
return $comment;
}
示例12: buildEntity
/**
* {@inheritdoc}
*/
public function buildEntity(array $form, FormStateInterface $form_state)
{
/** @var \Drupal\support_ticket\SupportTicketInterface $entity */
$entity = parent::buildEntity($form, $form_state);
if (!empty($form_state->getValue('uid')[0]['target_id']) && ($account = User::load($form_state->getValue('uid')[0]['target_id']))) {
$entity->setOwnerId($account->id());
} else {
$entity->setOwnerId(0);
}
return $entity;
}
示例13: buildEntity
/**
* Overrides EntityForm::buildEntity().
*/
public function buildEntity(array $form, array &$form_state)
{
$comment = parent::buildEntity($form, $form_state);
if (!empty($form_state['values']['date']) && $form_state['values']['date'] instanceof DrupalDateTime) {
$comment->setCreatedTime($form_state['values']['date']->getTimestamp());
} else {
$comment->setCreatedTime(REQUEST_TIME);
}
$comment->changed->value = REQUEST_TIME;
return $comment;
}
示例14: buildEntity
/**
* {@inheritdoc}
*/
public function buildEntity(array $form, FormStateInterface $form_state)
{
/** @var \Drupal\menu_link_content\MenuLinkContentInterface $entity */
$entity = parent::buildEntity($form, $form_state);
list($menu_name, $parent) = explode(':', $form_state->getValue('menu_parent'), 2);
$entity->parent->value = $parent;
$entity->menu_name->value = $menu_name;
$entity->enabled->value = !$form_state->isValueEmpty(array('enabled', 'value'));
$entity->expanded->value = !$form_state->isValueEmpty(array('expanded', 'value'));
return $entity;
}
示例15: buildEntity
/**
* {@inheritdoc}
*/
public function buildEntity(array $form, array &$form_state)
{
/** @var \Drupal\node\NodeInterface $entity */
$entity = parent::buildEntity($form, $form_state);
// A user might assign the node author by entering a user name in the node
// form, which we then need to translate to a user ID.
if (!empty($form_state['values']['uid']) && ($account = user_load_by_name($form_state['values']['uid']))) {
$entity->setOwnerId($account->id());
} else {
$entity->setOwnerId(0);
}
if (!empty($form_state['values']['created']) && $form_state['values']['created'] instanceof DrupalDateTime) {
$entity->setCreatedTime($form_state['values']['created']->getTimestamp());
} else {
$entity->setCreatedTime(REQUEST_TIME);
}
return $entity;
}