PageRenderTime 49ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/core/modules/views/src/Plugin/views/query/QueryPluginBase.php

https://gitlab.com/reasonat/test8
PHP | 353 lines | 125 code | 44 blank | 184 comment | 12 complexity | fbc4b456aa200cf4a82d683e7416f619 MD5 | raw file
  1. <?php
  2. namespace Drupal\views\Plugin\views\query;
  3. use Drupal\Core\Cache\Cache;
  4. use Drupal\Core\Cache\CacheableDependencyInterface;
  5. use Drupal\Core\Form\FormStateInterface;
  6. use Drupal\views\Plugin\views\PluginBase;
  7. use Drupal\views\ViewExecutable;
  8. use Drupal\views\Views;
  9. /**
  10. * @defgroup views_query_plugins Views query plugins
  11. * @{
  12. * Plugins for views queries.
  13. *
  14. * Query plugins generate and execute a built query object against a
  15. * particular storage backend, converting the Views query object into an
  16. * actual query. Although query plugins need not necessarily use SQL, most
  17. * other handler plugins that affect the query (fields, filters, etc.)
  18. * implicitly assume that the query is using SQL.
  19. *
  20. * Query plugins extend \Drupal\views\Plugin\views\query\QueryPluginBase.
  21. * They must be annotated with \Drupal\views\Annotation\ViewsQuery
  22. * annotation, and they must be in namespace directory Plugin\views\query.
  23. *
  24. * @ingroup views_plugins
  25. * @see plugin_api
  26. */
  27. /**
  28. * Base plugin class for Views queries.
  29. */
  30. abstract class QueryPluginBase extends PluginBase implements CacheableDependencyInterface {
  31. /**
  32. * A pager plugin that should be provided by the display.
  33. *
  34. * @var views_plugin_pager
  35. */
  36. public $pager = NULL;
  37. /**
  38. * Stores the limit of items that should be requested in the query.
  39. *
  40. * @var int
  41. */
  42. protected $limit;
  43. /**
  44. * Generate a query and a countquery from all of the information supplied
  45. * to the object.
  46. *
  47. * @param $get_count
  48. * Provide a countquery if this is true, otherwise provide a normal query.
  49. */
  50. public function query($get_count = FALSE) { }
  51. /**
  52. * Let modules modify the query just prior to finalizing it.
  53. *
  54. * @param view $view
  55. * The view which is executed.
  56. */
  57. public function alter(ViewExecutable $view) { }
  58. /**
  59. * Builds the necessary info to execute the query.
  60. *
  61. * @param view $view
  62. * The view which is executed.
  63. */
  64. public function build(ViewExecutable $view) { }
  65. /**
  66. * Executes the query and fills the associated view object with according
  67. * values.
  68. *
  69. * Values to set: $view->result, $view->total_rows, $view->execute_time,
  70. * $view->pager['current_page'].
  71. *
  72. * $view->result should contain an array of objects. The array must use a
  73. * numeric index starting at 0.
  74. *
  75. * @param view $view
  76. * The view which is executed.
  77. */
  78. public function execute(ViewExecutable $view) { }
  79. /**
  80. * Add a signature to the query, if such a thing is feasible.
  81. *
  82. * This signature is something that can be used when perusing query logs to
  83. * discern where particular queries might be coming from.
  84. *
  85. * @param view $view
  86. * The view which is executed.
  87. */
  88. public function addSignature(ViewExecutable $view) { }
  89. /**
  90. * Get aggregation info for group by queries.
  91. *
  92. * If NULL, aggregation is not allowed.
  93. */
  94. public function getAggregationInfo() { }
  95. public function validateOptionsForm(&$form, FormStateInterface $form_state) { }
  96. public function submitOptionsForm(&$form, FormStateInterface $form_state) { }
  97. public function summaryTitle() {
  98. return $this->t('Settings');
  99. }
  100. /**
  101. * {@inheritdoc}
  102. */
  103. public function calculateDependencies() {
  104. $dependencies = [];
  105. foreach ($this->getEntityTableInfo() as $entity_type => $info) {
  106. if (!empty($info['provider'])) {
  107. $dependencies['module'][] = $info['provider'];
  108. }
  109. }
  110. return $dependencies;
  111. }
  112. /**
  113. * Set a LIMIT on the query, specifying a maximum number of results.
  114. */
  115. public function setLimit($limit) {
  116. $this->limit = $limit;
  117. }
  118. /**
  119. * Set an OFFSET on the query, specifying a number of results to skip
  120. */
  121. public function setOffset($offset) {
  122. $this->offset = $offset;
  123. }
  124. /**
  125. * Returns the limit of the query.
  126. */
  127. public function getLimit() {
  128. return $this->limit;
  129. }
  130. /**
  131. * Create a new grouping for the WHERE or HAVING clause.
  132. *
  133. * @param $type
  134. * Either 'AND' or 'OR'. All items within this group will be added
  135. * to the WHERE clause with this logical operator.
  136. * @param $group
  137. * An ID to use for this group. If unspecified, an ID will be generated.
  138. * @param $where
  139. * 'where' or 'having'.
  140. *
  141. * @return
  142. * The group ID generated.
  143. */
  144. public function setWhereGroup($type = 'AND', $group = NULL, $where = 'where') {
  145. // Set an alias.
  146. $groups = &$this->$where;
  147. if (!isset($group)) {
  148. $group = empty($groups) ? 1 : max(array_keys($groups)) + 1;
  149. }
  150. // Create an empty group
  151. if (empty($groups[$group])) {
  152. $groups[$group] = array('conditions' => array(), 'args' => array());
  153. }
  154. $groups[$group]['type'] = strtoupper($type);
  155. return $group;
  156. }
  157. /**
  158. * Control how all WHERE and HAVING groups are put together.
  159. *
  160. * @param $type
  161. * Either 'AND' or 'OR'
  162. */
  163. public function setGroupOperator($type = 'AND') {
  164. $this->groupOperator = strtoupper($type);
  165. }
  166. /**
  167. * Loads all entities contained in the passed-in $results.
  168. *.
  169. * If the entity belongs to the base table, then it gets stored in
  170. * $result->_entity. Otherwise, it gets stored in
  171. * $result->_relationship_entities[$relationship_id];
  172. *
  173. * Query plugins that don't support entities can leave the method empty.
  174. */
  175. function loadEntities(&$results) {}
  176. /**
  177. * Returns a Unix timestamp to database native timestamp expression.
  178. *
  179. * @param string $field
  180. * The query field that will be used in the expression.
  181. *
  182. * @return string
  183. * An expression representing a timestamp with time zone.
  184. */
  185. public function getDateField($field) {
  186. return $field;
  187. }
  188. /**
  189. * Set the database to the current user timezone,
  190. *
  191. * @return string
  192. * The current timezone as returned by drupal_get_user_timezone().
  193. */
  194. public function setupTimezone() {
  195. return drupal_get_user_timezone();
  196. }
  197. /**
  198. * Creates cross-database date formatting.
  199. *
  200. * @param string $field
  201. * An appropriate query expression pointing to the date field.
  202. * @param string $format
  203. * A format string for the result, like 'Y-m-d H:i:s'.
  204. * @param bool $string_date
  205. * For certain databases, date format functions vary depending on string or
  206. * numeric storage.
  207. *
  208. * @return string
  209. * A string representing the field formatted as a date in the format
  210. * specified by $format.
  211. */
  212. public function getDateFormat($field, $format, $string_date = FALSE) {
  213. return $field;
  214. }
  215. /**
  216. * Returns an array of all tables from the query that map to an entity type.
  217. *
  218. * Includes the base table and all relationships, if eligible.
  219. *
  220. * Available keys for each table:
  221. * - base: The actual base table (i.e. "user" for an author relationship).
  222. * - relationship_id: The id of the relationship, or "none".
  223. * - alias: The alias used for the relationship.
  224. * - entity_type: The entity type matching the base table.
  225. * - revision: A boolean that specifies whether the table is a base table or
  226. * a revision table of the entity type.
  227. *
  228. * @return array
  229. * An array of table information, keyed by table alias.
  230. */
  231. public function getEntityTableInfo() {
  232. // Start with the base table.
  233. $entity_tables = array();
  234. $views_data = Views::viewsData();
  235. $base_table = $this->view->storage->get('base_table');
  236. $base_table_data = $views_data->get($base_table);
  237. if (isset($base_table_data['table']['entity type'])) {
  238. $entity_tables[$base_table_data['table']['entity type']] = array(
  239. 'base' => $base_table,
  240. 'alias' => $base_table,
  241. 'relationship_id' => 'none',
  242. 'entity_type' => $base_table_data['table']['entity type'],
  243. 'revision' => $base_table_data['table']['entity revision'],
  244. );
  245. // Include the entity provider.
  246. if (!empty($base_table_data['table']['provider'])) {
  247. $entity_tables[$base_table_data['table']['entity type']]['provider'] = $base_table_data['table']['provider'];
  248. }
  249. }
  250. // Include all relationships.
  251. foreach ((array) $this->view->relationship as $relationship_id => $relationship) {
  252. $table_data = $views_data->get($relationship->definition['base']);
  253. if (isset($table_data['table']['entity type'])) {
  254. // If this is not one of the entity base tables, skip it.
  255. $entity_type = \Drupal::entityTypeManager()->getDefinition($table_data['table']['entity type']);
  256. $entity_base_tables = [$entity_type->getBaseTable(), $entity_type->getDataTable(), $entity_type->getRevisionTable(), $entity_type->getRevisionDataTable()];
  257. if (!in_array($relationship->definition['base'], $entity_base_tables)) {
  258. continue;
  259. }
  260. $entity_tables[$relationship_id . '__' . $relationship->tableAlias] = array(
  261. 'base' => $relationship->definition['base'],
  262. 'relationship_id' => $relationship_id,
  263. 'alias' => $relationship->alias,
  264. 'entity_type' => $table_data['table']['entity type'],
  265. 'revision' => $table_data['table']['entity revision'],
  266. );
  267. // Include the entity provider.
  268. if (!empty($table_data['table']['provider'])) {
  269. $entity_tables[$relationship_id . '__' . $relationship->tableAlias]['provider'] = $table_data['table']['provider'];
  270. }
  271. }
  272. }
  273. // Determine which of the tables are revision tables.
  274. foreach ($entity_tables as $table_alias => $table) {
  275. $entity_type = \Drupal::entityManager()->getDefinition($table['entity_type']);
  276. if ($entity_type->getRevisionTable() == $table['base']) {
  277. $entity_tables[$table_alias]['revision'] = TRUE;
  278. }
  279. }
  280. return $entity_tables;
  281. }
  282. /**
  283. * {@inheritdoc}
  284. */
  285. public function getCacheMaxAge() {
  286. return Cache::PERMANENT;
  287. }
  288. /**
  289. * {@inheritdoc}
  290. */
  291. public function getCacheContexts() {
  292. $contexts = [];
  293. if (($views_data = Views::viewsData()->get($this->view->storage->get('base_table'))) && !empty($views_data['table']['entity type'])) {
  294. $entity_type_id = $views_data['table']['entity type'];
  295. $entity_type = \Drupal::entityManager()->getDefinition($entity_type_id);
  296. $contexts = $entity_type->getListCacheContexts();
  297. }
  298. return $contexts;
  299. }
  300. /**
  301. * {@inheritdoc}
  302. */
  303. public function getCacheTags() {
  304. return [];
  305. }
  306. }
  307. /**
  308. * @}
  309. */