/** * {@inheritdoc} */ public function validateReferenceableNewEntities(array $entities) { $entities = parent::validateReferenceableNewEntities($entities); $entities = array_filter($entities, function ($file) { /** @var \Drupal\file\FileInterface $file */ return $file->isPermanent() || $file->getOwnerId() === $this->currentUser->id(); }); return $entities; }
/** * {@inheritdoc} */ protected function buildEntityQuery($match = NULL, $match_operator = 'CONTAINS') { $query = parent::buildEntityQuery($match, $match_operator); // Adding the 'support_ticket_access' tag is sadly insufficient for support // tickets: core requires us to also know about the concept of 'published' and // 'unpublished'. $query->condition('status', SUPPORT_TICKET_PUBLISHED); return $query; }
/** * {@inheritdoc} */ protected function buildEntityQuery($match = NULL, $match_operator = 'CONTAINS') { $query = parent::buildEntityQuery($match, $match_operator); // Adding the 'comment_access' tag is sadly insufficient for comments: // core requires us to also know about the concept of 'published' and // 'unpublished'. if (!$this->currentUser->hasPermission('administer comments')) { $query->condition('status', CommentInterface::PUBLISHED); } return $query; }
/** * {@inheritdoc} */ public function validateReferenceableNewEntities(array $entities) { $entities = parent::validateReferenceableNewEntities($entities); // Mirror the conditions checked in buildEntityQuery(). if (!$this->currentUser->hasPermission('administer comments')) { $entities = array_filter($entities, function ($comment) { /** @var \Drupal\comment\CommentInterface $comment */ return $comment->isPublished(); }); } return $entities; }
/** * {@inheritdoc} */ protected function buildEntityQuery($match = NULL, $match_operator = 'CONTAINS') { $query = parent::buildEntityQuery($match, $match_operator); // The 'zone' zone member needs to be able to exclude the parent zone // from selection. It does this by passing a custom skip_id parameter // to the entity_autocomplete form element via #handler_settings. $handler_settings = $this->configuration['handler_settings']; if (!empty($handler_settings['skip_id'])) { $query->condition('id', $handler_settings['skip_id'], '<>'); } return $query; }
/** * {@inheritdoc} */ public function validateReferenceableNewEntities(array $entities) { $entities = parent::validateReferenceableNewEntities($entities); // Mirror the conditions checked in buildEntityQuery(). if (!$this->currentUser->hasPermission('bypass node access') && !count($this->moduleHandler->getImplementations('node_grants'))) { $entities = array_filter($entities, function ($node) { /** @var \Drupal\node\NodeInterface $node */ return $node->isPublished(); }); } return $entities; }
/** * {@inheritdoc} */ protected function buildEntityQuery($match = NULL, $match_operator = 'CONTAINS') { $query = parent::buildEntityQuery($match, $match_operator); // Adding the 'node_access' tag is sadly insufficient for nodes: core // requires us to also know about the concept of 'published' and // 'unpublished'. We need to do that as long as there are no access control // modules in use on the site. As long as one access control module is there, // it is supposed to handle this check. if (!$this->currentUser->hasPermission('bypass node access') && !count($this->moduleHandler->getImplementations('node_grants'))) { $query->condition('status', NODE_PUBLISHED); } return $query; }
/** * {@inheritdoc} */ public function getReferenceableEntities($match = NULL, $match_operator = 'CONTAINS', $limit = 0) { if ($match || $limit) { return parent::getReferenceableEntities($match, $match_operator, $limit); } $options = array(); $bundles = $this->entityManager->getBundleInfo('taxonomy_term'); $handler_settings = $this->configuration['handler_settings']; $bundle_names = !empty($handler_settings['target_bundles']) ? $handler_settings['target_bundles'] : array_keys($bundles); foreach ($bundle_names as $bundle) { if ($vocabulary = Vocabulary::load($bundle)) { if ($terms = $this->entityManager->getStorage('taxonomy_term')->loadTree($vocabulary->id(), 0, NULL, TRUE)) { foreach ($terms as $term) { $options[$vocabulary->id()][$term->id()] = str_repeat('-', $term->depth) . Html::escape($term->getName()); } } } } return $options; }
/** * {@inheritdoc} */ public function buildEntityQuery($match = NULL, $match_operator = 'CONTAINS') { $query = parent::buildEntityQuery($match, $match_operator); // Let administrators do anything. if ($this->currentUser->hasPermission('administer domains')) { return $query; } // Can this user access inactive domains? if (!$this->currentUser->hasPermission('access inactive domains')) { $query->condition('status', 1); } // Filter domains by the user's assignments, which are controlled by other // modules. Those modules must know what type of entity they are dealing // with, so look up the entity type and bundle. $info = $query->getMetadata('entity_reference_selection_handler'); $context['entity_type'] = $info->configuration['entity']->getEntityTypeId(); $context['bundle'] = $info->configuration['entity']->bundle(); // Load the current user. $account = User::load($this->currentUser->id()); // Run the alter hook. $this->moduleHandler->alter('domain_references', $query, $account, $context); return $query; }
/** * {@inheritdoc} */ public function getReferenceableEntities($match = NULL, $match_operator = 'CONTAINS', $limit = 0) { // No input, return everything from the entity query. if ($match === NULL || $match === '') { return parent::getReferenceableEntities($match, $match_operator, $limit); } // Start with the selection results returned by the entity query. Don't use // any limit because we have to apply a limit after filtering the items. $options = parent::getReferenceableEntities($match, $match_operator); // Always use a case-insensitive, escaped match. Entity labels returned by // SelectionInterface::getReferenceableEntities() are already escaped, so // the incoming $match needs to be escaped as well, making the comparison // possible. // @see \Drupal\Core\Entity\EntityReferenceSelection\SelectionInterface::getReferenceableEntities() if (is_string($match)) { $match = Html::escape(Unicode::strtolower($match)); } elseif (is_array($match)) { array_walk($match, function (&$item) { $item = Html::escape(Unicode::strtolower($item)); }); } $filtered = []; $count = 0; // Filter target entities by the output of their label() method. foreach ($options as $bundle => &$items) { foreach ($items as $entity_id => $label) { if ($this->matchLabel($match, $match_operator, $label)) { $filtered[$bundle][$entity_id] = $label; $count++; if ($limit && $count >= $limit) { break 2; } } } } return $filtered; }
/** * {@inheritdoc} */ public function validateReferenceableNewEntities(array $entities) { $entities = parent::validateReferenceableNewEntities($entities); // Mirror the conditions checked in buildEntityQuery(). if (!empty($this->configuration['handler_settings']['filter']['role'])) { $entities = array_filter($entities, function ($user) { /** @var \Drupal\user\UserInterface $user */ return !empty(array_intersect($user->getRoles(), $this->configuration['handler_settings']['filter']['role'])); }); } if (!$this->currentUser->hasPermission('administer users')) { $entities = array_filter($entities, function ($user) { /** @var \Drupal\user\UserInterface $user */ return $user->isActive(); }); } return $entities; }
/** * {@inheritdoc} */ protected function buildEntityQuery($match = NULL, $match_operator = 'CONTAINS') { $query = parent::buildEntityQuery($match, $match_operator); // The user entity doesn't have a label column. if (isset($match)) { $query->condition('name', $match, $match_operator); } // Filter by role. $handler_settings = $this->configuration['handler_settings']; if (!empty($handler_settings['filter']['role'])) { $query->condition('roles', $handler_settings['filter']['role'], 'IN'); } // Adding the permission check is sadly insufficient for users: core // requires us to also know about the concept of 'blocked' and 'active'. if (!$this->currentUser->hasPermission('administer users')) { $query->condition('status', 1); } return $query; }
/** * {@inheritdoc} */ protected function buildEntityQuery($match = NULL, $match_operator = 'CONTAINS') { $query = parent::buildEntityQuery($match, $match_operator); $query->condition('status', FILE_STATUS_PERMANENT); return $query; }