本文整理汇总了PHP中Drupal\Core\Session\AccountInterface::id方法的典型用法代码示例。如果您正苦于以下问题:PHP AccountInterface::id方法的具体用法?PHP AccountInterface::id怎么用?PHP AccountInterface::id使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Drupal\Core\Session\AccountInterface
的用法示例。
在下文中一共展示了AccountInterface::id方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: testUserTokens
function testUserTokens()
{
// Add a user picture to the account.
$image = current($this->drupalGetTestFiles('image'));
$edit = array('files[user_picture_0]' => drupal_realpath($image->uri));
$this->drupalPostForm('user/' . $this->account->id() . '/edit', $edit, t('Save'));
$storage = \Drupal::entityManager()->getStorage('user');
// Load actual user data from database.
$storage->resetCache();
$this->account = $storage->load($this->account->id());
$this->assertTrue(!empty($this->account->user_picture->target_id), 'User picture uploaded.');
$picture = ['#theme' => 'user_picture', '#account' => $this->account];
/** @var \Drupal\Core\Render\RendererInterface $renderer */
$renderer = \Drupal::service('renderer');
$user_tokens = array('picture' => $renderer->render($picture), 'picture:fid' => $this->account->user_picture->target_id, 'picture:size-raw' => 125, 'ip-address' => NULL, 'roles' => implode(', ', $this->account->getRoles()));
$this->assertTokens('user', array('user' => $this->account), $user_tokens);
// Remove the simpletest-created user role.
$roles = $this->account->getRoles();
$this->account->removeRole(end($roles));
$this->account->save();
// Remove the user picture field and reload the user.
FieldStorageConfig::loadByName('user', 'user_picture')->delete();
$storage->resetCache();
$this->account = $storage->load($this->account->id());
$user_tokens = array('picture' => NULL, 'picture:fid' => NULL, 'ip-address' => NULL, 'roles' => 'authenticated', 'roles:keys' => (string) DRUPAL_AUTHENTICATED_RID);
$this->assertTokens('user', array('user' => $this->account), $user_tokens);
// The ip address token should work for the current user token type.
$tokens = array('ip-address' => \Drupal::request()->getClientIp());
$this->assertTokens('current-user', array(), $tokens);
$anonymous = new AnonymousUserSession();
$tokens = array('roles' => 'anonymous', 'roles:keys' => (string) DRUPAL_ANONYMOUS_RID);
$this->assertTokens('user', array('user' => $anonymous), $tokens);
}
示例2: log
/**
* {@inheritdoc}
*/
public function log($level, $message, array $context = array())
{
// Merge in defaults.
$context += array('channel' => $this->channel, 'link' => '', 'user' => NULL, 'uid' => 0, 'request_uri' => '', 'referer' => '', 'ip' => '', 'timestamp' => time());
// Some context values are only available when in a request context.
if ($this->requestStack && ($request = $this->requestStack->getCurrentRequest())) {
$context['request_uri'] = $request->getUri();
$context['referer'] = $request->headers->get('Referer', '');
$context['ip'] = $request->getClientIP();
try {
if ($this->currentUser) {
$context['user'] = $this->currentUser;
$context['uid'] = $this->currentUser->id();
}
} catch (\Exception $e) {
// An exception might be thrown if the database connection is not
// available or due to another unexpected reason. It is more important
// to log the error that we already have so any additional exceptions
// are ignored.
}
}
if (is_string($level)) {
// Convert to integer equivalent for consistency with RFC 5424.
$level = $this->levelTranslation[$level];
}
// Call all available loggers.
foreach ($this->sortLoggers() as $logger) {
$logger->log($level, $message, $context);
}
}
示例3: user
/**
* User object.
*
* @return \Drupal\moodle\Sql\User
*/
public function user()
{
// Static cache of already retrieved user data.
$data =& drupal_static(__METHOD__, array());
$user_cid = "moodle-user:{$this->user->id()}";
// If we do not have this user id in the static cache, check {cache_data}.
if (!isset($data[$user_cid])) {
$cache = $this->cacheBackend->get($user_cid);
if ($cache && $cache->data && isset($cache->data[$user_cid])) {
$data[$user_cid] = $cache->data[$user_cid];
}
}
// If nothing in the cache then retrieve it from the database.
if (!isset($data[$user_cid])) {
$user = new User();
$this->query();
$this->addFields();
$statement = $this->query->execute();
$statement->setFetchMode(\PDO::FETCH_INTO, $user);
$data[$user_cid] = $statement->fetch();
// Store the results for a day.
$this->cacheBackend->set($user_cid, $data, REQUEST_TIME + 86400);
}
return $data[$user_cid];
}
示例4: onKernelRequestMaintenance
/**
* Determine whether the page is configured to be offline.
*
* @param \Symfony\Component\HttpKernel\Event\GetResponseEvent $event
* The event to process.
*/
public function onKernelRequestMaintenance(GetResponseEvent $event)
{
$request = $event->getRequest();
$route_match = RouteMatch::createFromRequest($request);
$path = $request->attributes->get('_system_path');
if ($this->maintenanceMode->applies($route_match)) {
// If the site is offline, log out unprivileged users.
if ($this->account->isAuthenticated() && !$this->maintenanceMode->exempt($this->account)) {
user_logout();
// Redirect to homepage.
$event->setResponse(new RedirectResponse($this->url('<front>', [], ['absolute' => TRUE])));
return;
}
}
if ($this->account->isAuthenticated()) {
if ($path == 'user/login') {
// If the user is already logged in, redirect to their profile page.
$event->setResponse($this->redirect('entity.user.canonical', ['user' => $this->account->id()]));
return;
}
if ($path == 'user/register') {
// If the user is already registered, redirect to their edit page.
$event->setResponse(new RedirectResponse($this->url('entity.user.edit_form', ['user' => $this->account->id()], ['absolute' => TRUE])));
return;
}
}
}
示例5: onBlockActiveContext
/**
* {@inheritdoc}
*/
public function onBlockActiveContext(BlockContextEvent $event)
{
$current_user = $this->userStorage->load($this->account->id());
$context = new Context(new ContextDefinition('entity:user', $this->t('Current user')));
$context->setContextValue($current_user);
$event->setContext('user.current_user', $context);
}
示例6: testMetatag
/**
* Tests adding and editing values using metatag.
*/
public function testMetatag()
{
// Create a test entity.
$edit = ['name[0][value]' => 'Barfoo', 'user_id[0][target_id]' => 'foo (' . $this->adminUser->id() . ')'];
$this->drupalPostForm('entity_test/add', $edit, t('Save'));
$entities = entity_load_multiple_by_properties('entity_test', ['name' => 'Barfoo']);
$this->assertEqual(1, count($entities), 'Entity was saved');
$entity = reset($entities);
// Update the Global defaults and test them.
$values = array('keywords' => 'Purple monkey dishwasher');
$this->drupalPostForm('admin/structure/metatag_defaults/global', $values, 'Save');
$this->assertText('Saved the Global Metatag defaults.');
$this->drupalGet('entity_test/' . $entity->id());
$elements = $this->cssSelect('meta[name=keywords]');
$this->assertTrue(count($elements) === 1, 'Found keywords metatag from defaults');
$this->assertEqual((string) $elements[0]['content'], $values['keywords'], 'Default keywords applied');
// Tests metatags with urls work.
$edit = ['name[0][value]' => 'UrlTags', 'user_id[0][target_id]' => 'foo (' . $this->adminUser->id() . ')', 'field_metatag[0][open_graph][og_url]' => 'http://example.com/foo.html'];
$this->drupalPostForm('entity_test/add', $edit, t('Save'));
$entities = entity_load_multiple_by_properties('entity_test', ['name' => 'UrlTags']);
$this->assertEqual(1, count($entities), 'Entity was saved');
$entity = reset($entities);
$this->drupalGet('entity_test/' . $entity->id());
$elements = $this->cssSelect("meta[property='og:url']");
$this->assertTrue(count($elements) === 1, 'Found keywords metatag from defaults');
$this->assertEqual((string) $elements[0]['content'], $edit['field_metatag[0][open_graph][og_url]']);
}
示例7: onKernelRequestMaintenance
/**
* Determine whether the page is configured to be offline.
*
* @param \Symfony\Component\HttpKernel\Event\GetResponseEvent $event
* The event to process.
*/
public function onKernelRequestMaintenance(GetResponseEvent $event)
{
$request = $event->getRequest();
$route_match = RouteMatch::createFromRequest($request);
$path = $request->attributes->get('_system_path');
if ($this->maintenanceMode->applies($route_match)) {
// If the site is offline, log out unprivileged users.
if ($this->account->isAuthenticated() && !$this->maintenanceMode->exempt($this->account)) {
user_logout();
// Redirect to homepage.
$event->setResponse(new RedirectResponse($this->url('<front>', [], ['absolute' => TRUE])));
return;
}
if ($this->account->isAnonymous() && $path == 'user') {
// Forward anonymous user to login page.
$event->setResponse(new RedirectResponse($this->url('user.login', [], ['absolute' => TRUE])));
return;
}
}
if ($this->account->isAuthenticated()) {
if ($path == 'user/login') {
// If user is logged in, redirect to 'user' instead of giving 403.
$event->setResponse(new RedirectResponse($this->url('user.page', [], ['absolute' => TRUE])));
return;
}
if ($path == 'user/register') {
// Authenticated user should be redirected to user edit page.
$event->setResponse(new RedirectResponse($this->url('entity.user.edit_form', ['user' => $this->account->id()], ['absolute' => TRUE])));
return;
}
}
}
示例8: determineBlockContext
/**
* {@inheritdoc}
*/
protected function determineBlockContext()
{
$current_user = $this->userStorage->load($this->account->id());
$context = new Context(new ContextDefinition('entity:user', $this->t('Current user')));
$context->setContextValue($current_user);
$this->addContext('current_user', $context);
}
示例9: executeMultiple
/**
* {@inheritdoc}
*/
public function executeMultiple(array $entities)
{
$info = [];
/** @var \Drupal\tmgmt_local\LocalTaskInterface $task */
foreach ($entities as $task) {
$info[$task->id()] = $task;
}
$this->tempStore->set($this->currentUser->id(), $info);
}
示例10: executeMultiple
/**
* {@inheritdoc}
*/
public function executeMultiple(array $entities)
{
$info = [];
/** @var \Drupal\support_ticket\SupportTicketInterface $support_ticket */
foreach ($entities as $support_ticket) {
$langcode = $support_ticket->language()->getId();
$info[$support_ticket->id()][$langcode] = $langcode;
}
$this->tempStore->set($this->currentUser->id(), $info);
}
示例11: executeMultiple
/**
* {@inheritdoc}
*/
public function executeMultiple(array $entities)
{
$info = [];
/** @var \Drupal\node\NodeInterface $node */
foreach ($entities as $node) {
$langcode = $node->language()->getId();
$info[$node->id()][$langcode] = $langcode;
}
$this->tempStore->set($this->currentUser->id(), $info);
}
示例12: executeMultiple
/**
* {@inheritdoc}
*/
public function executeMultiple(array $entities)
{
/** @var \Drupal\Core\Entity\ContentEntityInterface[] $entities */
$selection = [];
foreach ($entities as $entity) {
$langcode = $entity->language()->getId();
$selection[$entity->id()][$langcode] = $langcode;
}
$this->tempStore->set($this->currentUser->id(), $selection);
}
示例13: getRuntimeContexts
/**
* {@inheritdoc}
*/
public function getRuntimeContexts(array $unqualified_context_ids)
{
$current_user = $this->userStorage->load($this->account->id());
$context = new Context(new ContextDefinition('entity:user', $this->t('Current user')), $current_user);
$cacheability = new CacheableMetadata();
$cacheability->setCacheContexts(['user']);
$context->addCacheableDependency($cacheability);
$result = ['current_user' => $context];
return $result;
}
示例14: hook_node_grants
/**
* Inform the node access system what permissions the user has.
*
* This hook is for implementation by node access modules. In this hook,
* the module grants a user different "grant IDs" within one or more
* "realms". In hook_node_access_records(), the realms and grant IDs are
* associated with permission to view, edit, and delete individual nodes.
*
* The realms and grant IDs can be arbitrarily defined by your node access
* module; it is common to use role IDs as grant IDs, but that is not required.
* Your module could instead maintain its own list of users, where each list has
* an ID. In that case, the return value of this hook would be an array of the
* list IDs that this user is a member of.
*
* A node access module may implement as many realms as necessary to properly
* define the access privileges for the nodes. Note that the system makes no
* distinction between published and unpublished nodes. It is the module's
* responsibility to provide appropriate realms to limit access to unpublished
* content.
*
* Node access records are stored in the {node_access} table and define which
* grants are required to access a node. There is a special case for the view
* operation -- a record with node ID 0 corresponds to a "view all" grant for
* the realm and grant ID of that record. If there are no node access modules
* enabled, the core node module adds a node ID 0 record for realm 'all'. Node
* access modules can also grant "view all" permission on their custom realms;
* for example, a module could create a record in {node_access} with:
* @code
* $record = array(
* 'nid' => 0,
* 'gid' => 888,
* 'realm' => 'example_realm',
* 'grant_view' => 1,
* 'grant_update' => 0,
* 'grant_delete' => 0,
* );
* db_insert('node_access')->fields($record)->execute();
* @endcode
* And then in its hook_node_grants() implementation, it would need to return:
* @code
* if ($op == 'view') {
* $grants['example_realm'] = array(888);
* }
* @endcode
* If you decide to do this, be aware that the node_access_rebuild() function
* will erase any node ID 0 entry when it is called, so you will need to make
* sure to restore your {node_access} record after node_access_rebuild() is
* called.
*
* For a detailed example, see node_access_example.module.
*
* @param \Drupal\Core\Session\AccountInterface $account
* The account object whose grants are requested.
* @param string $op
* The node operation to be performed, such as 'view', 'update', or 'delete'.
*
* @return array
* An array whose keys are "realms" of grants, and whose values are arrays of
* the grant IDs within this realm that this user is being granted.
*
* @see node_access_view_all_nodes()
* @see node_access_rebuild()
* @ingroup node_access
*/
function hook_node_grants(\Drupal\Core\Session\AccountInterface $account, $op)
{
if ($account->hasPermission('access private content')) {
$grants['example'] = array(1);
}
if ($account->id()) {
$grants['example_author'] = array($account->id());
}
return $grants;
}
示例15: executeMultiple
/**
* {@inheritdoc}
*/
public function executeMultiple(array $entities)
{
$info = [];
/** @var \Drupal\magic_cards\CardBaseInterface $card */
foreach ($entities as $card) {
$langcode = $card->language()->getId();
$info[$card->id()][$langcode] = $langcode;
}
$this->tempStore->set($this->currentUser->id(), $info);
}