class BrightcoveVideoSearch in Brightcove Video Connect 8.2
Same name and namespace in other branches
- 3.x src/Plugin/Search/BrightcoveVideoSearch.php \Drupal\brightcove\Plugin\Search\BrightcoveVideoSearch
Executes a keyword search for videos against the {brightcove_video} table.
Plugin annotation
@SearchPlugin(
id = "brightcove_video_search",
title = @Translation("Brightcove Video")
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\search\Plugin\SearchPluginBase implements RefinableCacheableDependencyInterface, ContainerFactoryPluginInterface, SearchInterface uses RefinableCacheableDependencyTrait
- class \Drupal\brightcove\Plugin\Search\BrightcoveVideoSearch implements AccessibleInterface, SearchInterface
- class \Drupal\search\Plugin\SearchPluginBase implements RefinableCacheableDependencyInterface, ContainerFactoryPluginInterface, SearchInterface uses RefinableCacheableDependencyTrait
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of BrightcoveVideoSearch
File
- src/
Plugin/ Search/ BrightcoveVideoSearch.php, line 23
Namespace
Drupal\brightcove\Plugin\SearchView source
class BrightcoveVideoSearch extends SearchPluginBase implements AccessibleInterface, SearchInterface {
/**
* Maximum number of video entities to return.
*/
const RESULT_LIMIT = 15;
/**
* The current user.
*
* @var \Drupal\Core\Session\AccountInterface
*/
protected $currentUser;
/**
* The database connection.
*
* @var \Drupal\Core\Database\Connection
*/
protected $database;
/**
* Brightcove Video entity storage.
*
* @var \Drupal\Core\Entity\EntityStorageInterface
*/
protected $brightcoveVideoStorage;
/**
* Creates a BrightcoveVideoSearch object.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $pluginId
* The plugin_id for the plugin instance.
* @param array $definition
* The plugin implementation definition.
* @param \Drupal\Core\Session\AccountInterface $currentUser
* The current user.
* @param \Drupal\Core\Database\Connection $database
* The database connection.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
*
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
*/
public function __construct(array $configuration, string $pluginId, array $definition, AccountInterface $currentUser, Connection $database, EntityTypeManagerInterface $entity_type_manager) {
parent::__construct($configuration, $pluginId, $definition);
$this->currentUser = $currentUser;
$this->database = $database;
$this->brightcoveVideoStorage = $entity_type_manager
->getStorage('brightcove_video');
$this
->addCacheTags([
'brightcove_videos_list',
]);
}
/**
* {@inheritdoc}
*/
public function access($operation = 'view', AccountInterface $account = NULL, $return_as_object = FALSE) {
$result = AccessResult::allowedIfHasPermissions($account, [
'view published brightcove videos',
'view unpublished brightcove videos',
], 'OR');
return $return_as_object ? $result : $result
->isAllowed();
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $pluginId, $definition) {
return new static($configuration, $pluginId, $definition, $container
->get('current_user'), $container
->get('database'), $container
->get('entity_type.manager'));
}
/**
* Executes the search.
*
* @return array
* A structured list of search results.
*
* @throws \Drupal\Core\Entity\EntityMalformedException
*/
public function execute() {
$results = [];
if ($this
->isSearchExecutable()) {
$results = $this
->prepareResults($this
->queryVideos(), $results);
}
return $results;
}
/**
* {@inheritdoc}
*/
public function getHelp() {
$help = [
'list' => [
'#theme' => 'item_list',
'#items' => [
$this
->t('Video search looks for videos using words and partial words from the name and description fields. Example: straw would match videos straw, strawmar, and strawberry.'),
$this
->t('You can use * as a wildcard within your keyword. Example: s*m would match videos that contains strawman, seem, and blossoming.'),
],
],
];
return $help;
}
/**
* Prepare the result set from the chosen entities.
*
* @param array $entities
* The entities to add to the passed results.
* @param array $results
* Initial result set to extend.
*
* @return array
* The combined results.
*
* @throws \Drupal\Core\Entity\EntityMalformedException
* May happen if the video has no URL available. Should not happen.
*/
protected function prepareResults(array $entities, array $results) {
/** @var \Drupal\Core\Entity\ContentEntityInterface $video */
foreach ($entities as $video) {
$url = $video
->toUrl()
->toString();
$result = [
'title' => $video
->getName(),
'link' => $url,
];
$this
->addCacheableDependency($video);
$results[] = $result;
}
return $results;
}
/**
* Query the matching video entities from the database.
*
* @return array
* An array of matching video entities.
*/
protected function queryVideos() {
// Get query from the storage.
$query = $this->brightcoveVideoStorage
->getQuery();
// Escape for LIKE matching.
$keys = $this->database
->escapeLike($this->keywords);
// Replace wildcards with MySQL/PostgreSQL wildcards.
$keys = preg_replace('!\\*+!', '%', $keys);
$like = "%{$keys}%";
$query
->condition($query
->orConditionGroup()
->condition('name', $like, 'LIKE')
->condition('description', $like, 'LIKE')
->condition('long_description', $like, 'LIKE')
->condition('related_link__title', $like, 'LIKE'));
// Restrict user's access based on video status.
// If the user cannot view the published nor the unpublished videos then the
// user would get an access denied to the page so this case shouldn't be
// checked here.
$statuses = [];
if ($this->currentUser
->hasPermission('view published brightcove videos')) {
$statuses[] = BrightcoveVideoInterface::PUBLISHED;
}
if ($this->currentUser
->hasPermission('view unpublished brightcove videos')) {
$statuses[] = BrightcoveVideoInterface::NOT_PUBLISHED;
}
// Sanity condition, SQL query doesn't like empty lists so add a NULL value
// if the user does not have either of the required permissions, this should
// never happen though.
if (empty($statuses)) {
$statuses[] = NULL;
}
$query
->condition('status', $statuses, 'IN');
$ids = $query
->sort('created', 'DESC')
->pager(static::RESULT_LIMIT)
->execute();
return $this->brightcoveVideoStorage
->loadMultiple($ids);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
BrightcoveVideoSearch:: |
protected | property | Brightcove Video entity storage. | |
BrightcoveVideoSearch:: |
protected | property | The current user. | |
BrightcoveVideoSearch:: |
protected | property | The database connection. | |
BrightcoveVideoSearch:: |
public | function |
Checks data value access. Overrides AccessibleInterface:: |
|
BrightcoveVideoSearch:: |
public static | function |
Creates an instance of the plugin. Overrides SearchPluginBase:: |
|
BrightcoveVideoSearch:: |
public | function |
Executes the search. Overrides SearchInterface:: |
|
BrightcoveVideoSearch:: |
public | function |
Returns the searching help. Overrides SearchPluginBase:: |
|
BrightcoveVideoSearch:: |
protected | function | Prepare the result set from the chosen entities. | |
BrightcoveVideoSearch:: |
protected | function | Query the matching video entities from the database. | |
BrightcoveVideoSearch:: |
constant | Maximum number of video entities to return. | ||
BrightcoveVideoSearch:: |
public | function |
Creates a BrightcoveVideoSearch object. Overrides PluginBase:: |
|
CacheableDependencyTrait:: |
protected | property | Cache contexts. | |
CacheableDependencyTrait:: |
protected | property | Cache max-age. | |
CacheableDependencyTrait:: |
protected | property | Cache tags. | |
CacheableDependencyTrait:: |
public | function | 3 | |
CacheableDependencyTrait:: |
public | function | 3 | |
CacheableDependencyTrait:: |
public | function | 3 | |
CacheableDependencyTrait:: |
protected | function | Sets cacheability; useful for value object constructors. | |
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
RefinableCacheableDependencyTrait:: |
public | function | 1 | |
RefinableCacheableDependencyTrait:: |
public | function | ||
RefinableCacheableDependencyTrait:: |
public | function | ||
RefinableCacheableDependencyTrait:: |
public | function | ||
SearchPluginBase:: |
protected | property | The keywords to use in a search. | |
SearchPluginBase:: |
protected | property | Array of attributes - usually from the request object. | |
SearchPluginBase:: |
protected | property | Array of parameters from the query string from the request. | |
SearchPluginBase:: |
public | function |
Executes the search and builds render arrays for the result items. Overrides SearchInterface:: |
1 |
SearchPluginBase:: |
public | function |
Builds the URL GET query parameters array for search. Overrides SearchInterface:: |
1 |
SearchPluginBase:: |
public | function |
Returns the currently set attributes (from the request). Overrides SearchInterface:: |
|
SearchPluginBase:: |
public | function |
Returns the currently set keywords of the plugin instance. Overrides SearchInterface:: |
|
SearchPluginBase:: |
public | function |
Returns the current parameters set using setSearch(). Overrides SearchInterface:: |
|
SearchPluginBase:: |
public | function |
Returns the search index type this plugin uses. Overrides SearchInterface:: |
2 |
SearchPluginBase:: |
public | function |
Verifies if the values set via setSearch() are valid and sufficient. Overrides SearchInterface:: |
2 |
SearchPluginBase:: |
public | function |
Alters the search form when being built for a given plugin. Overrides SearchInterface:: |
1 |
SearchPluginBase:: |
public | function |
Sets the keywords, parameters, and attributes to be used by execute(). Overrides SearchInterface:: |
1 |
SearchPluginBase:: |
public | function |
Provides a suggested title for a page of search results. Overrides SearchInterface:: |
|
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |