/** * {@inheritdoc} */ public function query() { $this->ensureMyTable(); $source = $this->tableAlias . '.source_language'; $target = $this->tableAlias . '.target_language'; // Add a new group for the language abilities, which are a set of source // and target language combinations. $this->query->setWhereGroup('OR', 'eligible'); // Return all language abilities for the current user. foreach (tmgmt_local_supported_language_pairs(NULL, array(\Drupal::currentUser()->id())) as $key => $ability) { $key = str_replace('-', '_', $key); $arguments = array(':source_' . $key => $ability['source_language'], ':target_' . $key => $ability['target_language']); $this->query->addWhereExpression('eligible', "{$source} = :source_{$key} AND {$target} = :target_{$key}", $arguments); } }
/** * @covers ::getCacheTags * @covers ::getAllEntities */ public function testGetCacheMaxAge() { $view = $this->prophesize('Drupal\\views\\ViewExecutable')->reveal(); $query = new Sql([], 'sql', []); $query->view = $view; $view->result = []; // Add a row with an entity. $row = new ResultRow(); $prophecy = $this->prophesize('Drupal\\Core\\Entity\\EntityInterface'); $prophecy->getCacheMaxAge()->willReturn(10); $entity = $prophecy->reveal(); $row->_entity = $entity; $view->result[] = $row; // Add a row with an entity and a relationship entity. $row = new ResultRow(); $prophecy = $this->prophesize('Drupal\\Core\\Entity\\EntityInterface'); $prophecy->getCacheMaxAge()->willReturn(20); $entity = $prophecy->reveal(); $row->_entity = $entity; $prophecy = $this->prophesize('Drupal\\Core\\Entity\\EntityInterface'); $prophecy->getCacheMaxAge()->willReturn(30); $entity = $prophecy->reveal(); $row->_relationship_entities[] = $entity; $prophecy = $this->prophesize('Drupal\\Core\\Entity\\EntityInterface'); $prophecy->getCacheMaxAge()->willReturn(40); $entity = $prophecy->reveal(); $row->_relationship_entities[] = $entity; $this->assertEquals(10, $query->getCacheMaxAge()); }
/** * {@inheritdoc} */ public function query() { $this->ensureMyTable(); // Therefore construct the join. // Add the join for the tmgmt_job_item table. $configuration = array('table' => 'tmgmt_job_item', 'field' => 'tjid', 'left_table' => $this->tableAlias, 'left_field' => 'tjid', 'operator' => '='); if (!empty($this->options['state'])) { $configuration['extra'] = [['field' => 'state', 'value' => $this->options['state']]]; } /** @var \Drupal\views\Plugin\views\join\Standard $join */ $join = Views::pluginManager('join')->createInstance('standard', $configuration); // Add the join to the tmgmt_job_item table. $this->tableAlias = $this->query->addTable('tmgmt_job_item', $this->relationship, $join); // And finally add the count of the job items field. $params = array('function' => 'count'); $this->field_alias = $this->query->addField($this->tableAlias, 'tjiid', NULL, $params); $this->addAdditionalFields(); }
/** * {@inheritdoc} */ public function query() { $key = reset($this->value); $field = $this->field; $table = $this->table; switch ($key) { case 'needs_review': $table_alias = 'job_item'; // Create a sub query to add the state of job item to the view. $sub_query = \Drupal::database()->select('tmgmt_job_item', $table_alias); $sub_query->addField($table_alias, 'tjid'); // Add a where clause to check if there are job items with state 2. $sub_query->condition("{$table_alias}.state", 2, '='); // Select all job items that are in the sub query. $this->query->addWhere($this->options['group'], 'tjid', $sub_query, 'IN'); $this->query->addWhere($this->options['group'], "{$table}.{$field}", '1', '='); break; case 'in_progress': $table_alias = 'job_item'; // Create a sub query to add the state of job item to the view. $sub_query = \Drupal::database()->select('tmgmt_job_item', $table_alias); $sub_query->addField($table_alias, 'tjid'); // Add a where clause to check if there are job items with state 2. $sub_query->condition("{$table_alias}.state", 2, '='); // Select all job items that are not in the sub query. $this->query->addWhere($this->options['group'], 'tjid', $sub_query, 'NOT IN'); $this->query->addWhere($this->options['group'], "{$table}.{$field}", '1', '='); break; case 'open_jobs': $this->query->addWhere($this->options['group'], "{$table}.{$field}", array(0, 1, 2, 3, 6), 'IN'); break; default: $this->query->addWhere($this->options['group'], "{$table}.{$field}", $key, '='); break; } }
/** * @covers ::loadEntities * @covers ::_assignEntitiesToResult */ public function testLoadEntitiesWithRelationshipAndRevision() { // We don't use prophecy, because prophecy enforces methods. $view = $this->getMockBuilder(ViewExecutable::class)->disableOriginalConstructor()->getMock(); $this->setupViewWithRelationships($view); $view_entity = $this->prophesize(ViewEntityInterface::class); $view_entity->get('base_table')->willReturn('entity_first__revision'); $view_entity->get('base_field')->willReturn('vid'); $view->storage = $view_entity->reveal(); $entities = ['second' => [11 => $this->prophesize(EntityInterface::class)->reveal(), 12 => $this->prophesize(EntityInterface::class)->reveal()]]; $entity_revisions = ['first' => [1 => $this->prophesize(EntityInterface::class)->reveal(), 3 => $this->prophesize(EntityInterface::class)->reveal()]]; $entity_type_manager = $this->setupEntityTypes($entities, $entity_revisions); $query = new Sql([], 'sql', [], $entity_type_manager->reveal()); $query->view = $view; $result = []; $result[] = new ResultRow(['vid' => 1, 'entity_second__id' => 11]); // Provide an explicit NULL value, to test the case of a non required // relationship. $result[] = new ResultRow(['vid' => 1, 'entity_second__id' => NULL]); $result[] = new ResultRow(['vid' => 3, 'entity_second__id' => 12]); $query->addField('entity_first__revision', 'vid', 'vid'); $query->addField('entity_second', 'id', 'entity_second__id'); $query->loadEntities($result); $this->assertSame($entity_revisions['first'][1], $result[0]->_entity); $this->assertSame($entity_revisions['first'][1], $result[1]->_entity); $this->assertSame($entity_revisions['first'][3], $result[2]->_entity); $this->assertSame($entities['second'][11], $result[0]->_relationship_entities['entity_second']); $this->assertEquals([], $result[1]->_relationship_entities); $this->assertSame($entities['second'][12], $result[2]->_relationship_entities['entity_second']); }