/** * {@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(); }
/** * @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']); }