Commit 57dc8767 authored by alexpott's avatar alexpott

Issue #2017207 by Berdir, marcingy: Complete conversion of users to Entity Field API.

parent 8242143e
......@@ -1485,7 +1485,7 @@ function comment_prepare_author(Comment $comment) {
if (empty($account->uid->value)) {
$account = entity_create('user', array('uid' => 0, 'name' => $comment->name->value, 'homepage' => $comment->homepage->value));
}
return $account->getBCEntity();
return $account;
}
/**
......
......@@ -217,7 +217,7 @@ function comment_tokens($type, $tokens, array $data = array(), array $options =
}
if (($author_tokens = $token_service->findwithPrefix($tokens, 'author')) && $account = $comment->uid->entity) {
$replacements += $token_service->generate('user', $author_tokens, array('user' => $account->getBCEntity()), $options);
$replacements += $token_service->generate('user', $author_tokens, array('user' => $account), $options);
}
}
elseif ($type == 'node' & !empty($data['node'])) {
......
......@@ -331,14 +331,16 @@ function contact_form_user_form_alter(&$form, &$form_state) {
'#default_value' => isset($account_data) ? $account_data : config('contact.settings')->get('user_default_enabled'),
'#description' => t('Allow other users to contact you via a personal contact form which keeps your e-mail address hidden. Note that some privileged users such as site administrators are still able to contact you even if you choose to disable this feature.'),
);
$form['actions']['submit']['#submit'][] = 'contact_user_profile_form_submit';
}
/**
* Implements hook_user_update().
* Submit callback for the user profile form to save the contact page setting.
*/
function contact_user_update($account) {
if (isset($account->contact)) {
Drupal::service('user.data')->set('contact', $account->id(), 'enabled', (int) $account->contact);
function contact_user_profile_form_submit($form, &$form_state) {
$account = $form_state['controller']->getEntity();
if ($account->id() && isset($form_state['values']['contact'])) {
Drupal::service('user.data')->set('contact', $account->id(), 'enabled', (int) $form_state['values']['contact']);
}
}
......
......@@ -165,7 +165,7 @@ public function save(array $form, array &$form_state) {
// Send to the user in the user's preferred language.
$to = $recipient->getEmail();
$recipient_langcode = $recipient->getPreferredLangcode();
$params['recipient'] = $recipient->getBCEntity();
$params['recipient'] = $recipient;
}
else {
throw new \RuntimeException(t('Unable to determine message recipient.'));
......
......@@ -54,7 +54,7 @@ public function render_link(EntityInterface $entity, ResultRow $values) {
$uid = $entity->id();
$path = "user/$uid/contact";
if (!_contact_personal_tab_access($entity->getBCEntity())) {
if (!_contact_personal_tab_access($entity)) {
return;
}
......
......@@ -165,6 +165,15 @@ function testPersonalContactAccess() {
$this->drupalLogin($this->web_user);
$this->drupalGet('user/' . $this->contact_user->id() . '/contact');
$this->assertResponse(403);
// Test enabling and disabling the contact page through the user profile
// form.
$this->drupalGet('user/' . $this->web_user->id() . '/edit');
$this->assertNoFieldChecked('edit-contact--2');
$this->assertFalse(\Drupal::service('user.data')->get('contact', $this->web_user->id(), 'enabled'), 'Personal contact form disabled');
$this->drupalPost(NULL, array('contact' => TRUE), t('Save'));
$this->assertFieldChecked('edit-contact--2');
$this->assertTrue(\Drupal::service('user.data')->get('contact', $this->web_user->id(), 'enabled'), 'Personal contact form enabled');
}
/**
......
......@@ -121,7 +121,7 @@ public function getChangedTime() {
* {@inheritdoc}
*/
public function getOwner() {
return $this->get('uid')->entity->getBCEntity();
return $this->get('uid')->entity;
}
/**
......
......@@ -96,8 +96,8 @@ function testRevisions() {
// Attach the second file to a user.
$user = $this->drupalCreateUser();
$user->{$field_name}[Language::LANGCODE_NOT_SPECIFIED][0]['target_id'] = $node_file_r3->id();
$user->{$field_name}[Language::LANGCODE_NOT_SPECIFIED][0]['display'] = 1;
$user->$field_name->target_id = $node_file_r3->id();
$user->$field_name->display = 1;
$user->save();
$this->drupalGet('user/' . $user->id() . '/edit');
......
......@@ -81,7 +81,7 @@ public function access(EntityInterface $node, $operation, $langcode, AccountInte
$query->range(0, 1);
$grants = $query->orConditionGroup();
foreach (node_access_grants($operation, $account instanceof User ? $account->getBCEntity() : $account) as $realm => $gids) {
foreach (node_access_grants($operation, $account) as $realm => $gids) {
foreach ($gids as $gid) {
$grants->condition(db_and()
->condition('gid', $gid)
......
......@@ -224,9 +224,9 @@ public function getAuthor() {
$entity = $this->get('uid')->entity;
// If no user is given, default to the anonymous user.
if (!$entity) {
return user_load(0)->getBCentity();
return user_load(0);
}
return $entity->getBCEntity();
return $entity;
}
/**
......
<?php
/**
* @file
* Contains \Drupal\overlay\Tests\OverlaySettingTest.
*/
namespace Drupal\overlay\Tests;
use Drupal\simpletest\WebTestBase;
/**
* Tests the overlay settings.
*/
class OverlaySettingTest extends WebTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('overlay');
public static function getInfo() {
return array(
'name' => 'Overlay settings',
'description' => 'Test that users can configure the overlay',
'group' => 'Overlay',
);
}
/**
* Test that users can configure the overlay.
*/
function testNodeCreation() {
$user = $this->drupalCreateUser(array('access overlay'));
$this->drupalLogin($user);
$this->drupalGet('user/' . $user->id() . '/edit');
$this->assertFieldChecked('edit-overlay');
$this->drupalPost(NULL, array('overlay' => FALSE), t('Save'));
$this->assertNoFieldChecked('edit-overlay');
$this->assertFalse(\Drupal::service('user.data')->get('overlay', $user->id(), 'enabled'), 'Overlay disabled');
}
}
......@@ -109,15 +109,17 @@ function overlay_form_user_form_alter(&$form, &$form_state) {
'#description' => t('Show administrative pages on top of the page you started from.'),
'#default_value' => isset($account_data) ? $account_data : 1,
);
$form['actions']['submit']['#submit'][] = 'overlay_user_profile_form_submit';
}
}
/**
* Implements hook_user_update().
* Submit callback for the user profile form to save the overlay page setting.
*/
function overlay_user_update($account) {
if (isset($account->overlay)) {
Drupal::service('user.data')->set('overlay', $account->id(), 'enabled', (int) $account->overlay);
function overlay_user_profile_form_submit($form, &$form_state) {
$account = $form_state['controller']->getEntity();
if ($account->id() && isset($form_state['values']['overlay'])) {
Drupal::service('user.data')->set('overlay', $account->id(), 'enabled', (int) $form_state['values']['overlay']);
}
}
......
......@@ -69,7 +69,7 @@ public function testUserPictureUpgrade() {
// Check the user picture and file usage record.
$user = user_load(1);
$file = file_load($user->user_picture[Language::LANGCODE_NOT_SPECIFIED][0]['target_id']);
$file = $user->user_picture->entity;
$this->assertEqual('public://user_pictures_dir/faked_image.png', $file->getFileUri());
$usage = file_usage()->listUsage($file);
$this->assertEqual(1, $usage['file']['user'][1]);
......
......@@ -7,16 +7,16 @@
namespace Drupal\user;
use Drupal\Core\Entity\EntityFormController;
use Drupal\Core\Entity\EntityFormControllerNG;
use Drupal\Core\Language\Language;
/**
* Form controller for the user account forms.
*/
abstract class AccountFormController extends EntityFormController {
abstract class AccountFormController extends EntityFormControllerNG {
/**
* Overrides Drupal\Core\Entity\EntityFormController::form().
* {@inheritdoc}
*/
public function form(array $form, array &$form_state) {
$account = $this->entity;
......@@ -235,9 +235,9 @@ public function buildEntity(array $form, array &$form_state) {
// 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 (empty($this->roles_filtered)) {
if (is_string(key($form_state['values']['roles']))) {
$form_state['values']['roles'] = array_keys(array_filter($form_state['values']['roles']));
$this->roles_filtered = TRUE;
}
return parent::buildEntity($form, $form_state);
}
......
......@@ -132,7 +132,7 @@ public function validateForm(array &$form, array &$form_state) {
* {@inheritdoc}
*/
public function submitForm(array &$form, array &$form_state) {
$account = $this->storageController->load($form_state['uid'])->getBCEntity();
$account = $this->storageController->load($form_state['uid']);
$form_state['redirect'] = 'user/' . $account->id();
user_login_finalize($account);
......
......@@ -133,7 +133,7 @@ public function submitForm(array &$form, array &$form_state) {
$account = $form_state['values']['account'];
// Mail one time login URL and instructions using current language.
$mail = _user_mail_notify('password_reset', $account->getBCEntity(), $langcode);
$mail = _user_mail_notify('password_reset', $account, $langcode);
if (!empty($mail)) {
watchdog('user', 'Password reset instructions mailed to %name at %email.', array('%name' => $account->getUsername(), '%email' => $account->getEmail()));
drupal_set_message(t('Further instructions have been sent to your e-mail address.'));
......
......@@ -12,9 +12,7 @@
use Drupal\Core\Entity\EntityNG;
use Drupal\Core\Entity\Annotation\EntityType;
use Drupal\Core\Annotation\Translation;
use Drupal\user\UserBCDecorator;
use Drupal\user\UserInterface;
use Drupal\Core\Language\Language;
/**
* Defines the user entity class.
......@@ -128,7 +126,7 @@ public function postSave(EntityStorageControllerInterface $storage_controller, $
if ($this->status->value != $this->original->status->value) {
// The user's status is changing; conditionally send notification email.
$op = $this->status->value == 1 ? 'status_activated' : 'status_blocked';
_user_mail_notify($op, $this->getBCEntity());
_user_mail_notify($op, $this);
}
}
else {
......@@ -148,19 +146,6 @@ public static function postDelete(EntityStorageControllerInterface $storage_cont
$storage_controller->deleteUserRoles($uids);
}
/**
* {@inheritdoc}
*/
public function getBCEntity() {
if (!isset($this->bcEntity)) {
// Initialize field definitions so that we can pass them by reference.
$this->getPropertyDefinitions();
$this->bcEntity = new UserBCDecorator($this, $this->fieldDefinitions);
}
return $this->bcEntity;
}
/**
* {@inheritdoc}
*/
......
......@@ -28,17 +28,7 @@ protected function actions(array $form, array &$form_state) {
}
/**
* Overrides Drupal\Core\Entity\EntityFormController::submit().
*/
public function submit(array $form, array &$form_state) {
// @todo Consider moving this into the parent method.
// Remove unneeded values.
form_state_values_clean($form_state);
parent::submit($form, $form_state);
}
/**
* Overrides Drupal\Core\Entity\EntityFormController::save().
* {@inheritdoc}
*/
public function save(array $form, array &$form_state) {
$account = $this->entity;
......
......@@ -66,7 +66,7 @@ public function testUserListing() {
$accounts['admin'] = entity_load('user', 1);
$this->drupalLogin($admin_user->getBCEntity());
$this->drupalLogin($admin_user);
$this->drupalGet('admin/people');
$this->assertResponse(200, 'The admin user has access to the user admin listing.');
......
......@@ -135,6 +135,6 @@ function saveUserPicture($image) {
// Load actual user data from database.
$account = user_load($this->web_user->id(), TRUE);
return file_load($account->user_picture[Language::LANGCODE_NOT_SPECIFIED][0]['target_id'], TRUE);
return file_load($account->user_picture->target_id, TRUE);
}
}
<?php
/**
* @file
* Contains \Drupal\user\UserBCDecorator.
*/
namespace Drupal\user;
use Drupal\Core\Entity\EntityBCDecorator;
/**
* Defines the user specific entity BC decorator.
*/
class UserBCDecorator extends EntityBCDecorator implements UserInterface {
/**
* The decorated user entity.
*
* @var \Drupal\user\UserInterface
*/
protected $decorated;
/**
* {@inheritdoc}
*/
public function &__get($name) {
// Special handling for roles, as the return value is expected to be an
// array.
if ($name == 'roles') {
$roles = $this->decorated->getRoles();
return $roles;
}
return parent::__get($name);
}
/**
* {@inheritdoc}
*/
public function getRoles() {
return $this->decorated->getRoles();
}
/**
* {@inheritdoc}
*/
public function hasPermission($permission) {
return $this->decorated->hasPermission($permission);
}
/**
* {@inheritdoc}
*/
public function getSecureSessionId() {
return $this->decorated->getSecureSessionId();
}
/**
* {@inheritdoc}
*/
public function getSessionData() {
return $this->decorated->getSecureSessionId();
}
/**
* {@inheritdoc}
*/
public function getSessionId() {
return $this->decorated->getSessionId();
}
/**
* {@inheritdoc}
*/
public function hasRole($rid) {
return $this->decorated->hasRole($rid);
}
/**
* {@inheritdoc}
*/
public function addRole($rid) {
$this->decorated->addRole($rid);
}
/**
* {@inheritdoc}
*/
public function removeRole($rid) {
$this->decorated->removeRole($rid);
}
/**
* {@inheritdoc}
*/
public function getPassword() {
return $this->decorated->getPassword();
}
/**
* {@inheritdoc}
*/
public function setPassword($password) {
return $this->decorated->setPassword($password);
}
/**
* {@inheritdoc}
*/
public function getEmail() {
return $this->decorated->getEmail();
}
/**
* {@inheritdoc}
*/
public function setEmail($mail) {
return $this->decorated->setEmail($mail);
}
/**
* {@inheritdoc}
*/
public function getDefaultTheme() {
return $this->decorated->getDefaultTheme();
}
/**
* {@inheritdoc}
*/
public function getSignature() {
return $this->decorated->getSignature();
}
/**
* {@inheritdoc}
*/
public function getSignatureFormat() {
return $this->decorated->getSignatureFormat();
}
/**
* {@inheritdoc}
*/
public function getCreatedTime() {
return $this->decorated->getCreatedTime();
}
/**
* {@inheritdoc}
*/
public function getLastAccessedTime() {
return $this->decorated->getLastAccessedTime();
}
/**
* {@inheritdoc}
*/
public function setLastAccessTime($timestamp) {
return $this->decorated->setLastAccessTime($timestamp);
}
/**
* {@inheritdoc}
*/
public function getLastLoginTime() {
return $this->decorated->getLastLoginTime();
}
/**
* {@inheritdoc}
*/
public function setLastLoginTime($timestamp) {
return $this->decorated->setLastLoginTime($timestamp);
}
/**
* {@inheritdoc}
*/
public function isActive() {
return $this->decorated->isActive();
}
/**
* {@inheritdoc}
*/
public function isBlocked() {
return $this->decorated->isBlocked();
}
/**
* {@inheritdoc}
*/
public function activate() {
return $this->decorated->activate();
}
/**
* {@inheritdoc}
*/
public function block() {
return $this->decorated->block();
}
/**
* {@inheritdoc}
*/
public function getTimeZone() {
return $this->decorated->getTimeZone();
}
/**
* {@inheritdoc}
*/
public function getPreferredLangcode($default = NULL) {
return $this->decorated->getPreferredLangcode($default);
}
/**
* {@inheritdoc}
*/
public function getPreferredAdminLangcode($default = NULL) {
return $this->decorated->getPreferredAdminLangcode($default);
}
/**
* {@inheritdoc}
*/
public function getInitialEmail() {
return $this->decorated->getInitialEmail();
}
/**
* {@inheritdoc}
*/
public function isAuthenticated() {
return $this->decorated->id() > 0;
}
/**
* {@inheritdoc}
*/
public function isAnonymous() {
return $this->decorated->id() == 0;
}
/**
* {@inheritdoc}
*/
public function getUsername() {
return $this->decorated->getUsername();
}
/**
* {@inheritdoc}
*/
public function setUsername($username) {
return $this->decorated->setUsername($username);
}
}
......@@ -96,23 +96,11 @@ function attachLoad(&$queried_users, $load_revision = FALSE) {
/**
* {@inheritdoc}
*/
public function create(array $values) {
return parent::create($values)->getBCEntity();
}
/**
* Overrides Drupal\Core\Entity\DatabaseStorageController::save().
*/
public function save(EntityInterface $entity) {
if (!$entity->id()) {
$entity->uid->value = $this->database->nextId($this->database->query('SELECT MAX(uid) FROM {users}')->fetchField());
$entity->enforceIsNew();
}
// There are some cases that pre-set ->original for performance. Make sure
// original is not a BC decorator.
if ($entity->original instanceof EntityBCDecorator) {
$entity->original = $entity->original->getNGEntity();
}
parent::save($entity);
}
......@@ -162,13 +150,13 @@ protected function invokeHook($hook, EntityInterface $entity) {
$function = 'field_attach_delete_revision';
}
if (!empty($this->entityInfo['fieldable']) && function_exists($function)) {
$function($entity->getBCEntity());
$function($entity);
}
// Invoke the hook.
\Drupal::moduleHandler()->invokeAll($this->entityType . '_' . $hook, array($entity->getBCEntity()));
\Drupal::moduleHandler()->invokeAll($this->entityType . '_' . $hook, array($entity));
// Invoke the respective entity-level hook.
\Drupal::moduleHandler()->invokeAll('entity_' . $hook, array($entity->getBCEntity(), $this->entityType));
\Drupal::moduleHandler()->invokeAll('entity_' . $hook, array($entity, $this->entityType));
}
/**
......
......@@ -258,12 +258,7 @@ function user_field_extra_fields() {
* @see \Drupal\Core\Entity\Query\QueryInterface
*/
function user_load_multiple(array $uids = NULL, $reset = FALSE) {
$entities = entity_load_multiple('user', $uids, $reset);
// Return BC-entities.
foreach ($entities as $id => $entity) {
$entities[$id] = $entity->getBCEntity();
}
return $entities;
return entity_load_multiple('user', $uids, $reset);
}
/**
......@@ -291,8 +286,7 @@ function user_load_multiple(array $uids = NULL, $reset = FALSE) {
* @see user_load_multiple()
*/
function user_load($uid, $reset = FALSE) {
$user = entity_load('user', $uid, $reset);
return $user ? $user->getBCEntity() : NULL;
return entity_load('user', $uid, $reset);
}
/**
......@@ -308,7 +302,7 @@ function user_load($uid, $reset = FALSE) {
*/
function user_load_by_mail($mail) {
$users = entity_load_multiple_by_properties('user', array('mail' => $mail));
return $users ? reset($users)->getBCEntity() : FALSE;
return $users ? reset($users) : FALSE;
}
/**
......@@ -324,7 +318,7 @@ function user_load_by_mail($mail) {
*/
function user_load_by_name($name) {
$users = entity_load_multiple_by_properties('user', array('name' => $name));
return $users ? reset($users)->getBCEntity() : FALSE;
return $users ? reset($users) : FALSE;
}
/**
......@@ -604,7 +598,8 @@ function user_validate_current_pass(&$form, &$form_state) {
// This validation only works for required textfields (like mail) or
// form values like password_confirm that have their own validation
// that prevent them from being empty if they are changed.
if ((strlen(trim($form_state['values'][$key])) > 0) && ($form_state['values'][$key] != $account->$key)) {
$current_value = $account->getPropertyDefinition($key) ? $account->get($key)->value : $account->$key;
if ((strlen(trim($form_state['values'][$key])) > 0) && ($form_state['values'][$key] != $current_value)) {
$current_pass_failed = empty($form_state['values']['current_pass']) || !drupal_container()->get('password')->check($form_state['values']['current_pass'], $account);
if ($current_pass_failed) {
form_set_error('current_pass', t("Your current password is missing or incorrect; it's required to change the %name.", array('%name' => $name)));
......