diff --git a/domain_source/config/schema/domain_source.views.schema.yml b/domain_source/config/schema/domain_source.views.schema.yml new file mode 100644 index 00000000..5ce71d58 --- /dev/null +++ b/domain_source/config/schema/domain_source.views.schema.yml @@ -0,0 +1,9 @@ +views.field.domain_source: + type: views_field + label: 'Domain source' +views.filter.domain_source: + type: views.filter.in_operator + label: 'Domain source' +views.filter_value.domain_source: + type: views.filter_value.in_operator + label: 'Domain source' diff --git a/domain_source/domain_source.module b/domain_source/domain_source.module index e98a2f4e..b4b50e84 100644 --- a/domain_source/domain_source.module +++ b/domain_source/domain_source.module @@ -184,3 +184,12 @@ function domain_source_form_node_form_alter(&$form, FormStateInterface $form_sta } } } + +/** + * Implements hook_views_data_alter. + */ +function domain_source_views_data_alter(array &$data) { + $table = 'node__' . DOMAIN_SOURCE_FIELD; + $data[$table][DOMAIN_SOURCE_FIELD]['field']['id'] = 'domain_source'; + $data[$table][DOMAIN_SOURCE_FIELD . '_target_id']['filter']['id'] = 'domain_source'; +} diff --git a/domain_source/src/Plugin/views/field/DomainSource.php b/domain_source/src/Plugin/views/field/DomainSource.php new file mode 100644 index 00000000..756acbe4 --- /dev/null +++ b/domain_source/src/Plugin/views/field/DomainSource.php @@ -0,0 +1,50 @@ +options['settings']['link'])) { + foreach ($items as &$item) { + $object = $item['raw']; + $entity = $object->getEntity(); + $url = $entity->toUrl()->toString(); + $domain = $item['rendered']['#options']['entity']; + $item['rendered']['#type'] = 'markup'; + $item['rendered']['#markup'] = '' . $domain->label() . ''; + } + uasort($items, array($this, 'sort')); + } + + return $items; + } + + /** + * Sort the domain list, if possible. + */ + private function sort($a, $b) { + $domainA = isset($a['rendered']['#options']['entity']) ? $a['rendered']['#options']['entity'] : 0; + $domainB = isset($b['rendered']['#options']['entity']) ? $b['rendered']['#options']['entity'] : 0; + if ($domainA !== 0) { + return $domainA->getWeight() > $domainB->getWeight(); + } + // We don't have a domain object so sort as best we can. + return $a['rendered']['#plain_text'] > $b['rendered']['#plain_text']; + } + +} diff --git a/domain_source/src/Plugin/views/filter/DomainSource.php b/domain_source/src/Plugin/views/filter/DomainSource.php new file mode 100644 index 00000000..522ea229 --- /dev/null +++ b/domain_source/src/Plugin/views/filter/DomainSource.php @@ -0,0 +1,41 @@ +valueOptions)) { + $this->valueTitle = $this->t('Domains'); + $this->valueOptions = [ + '_active' => $this->t('Active domain'), + ] + \Drupal::service('domain.loader')->loadOptionsList(); + } + return $this->valueOptions; + } + + /** + * {@inheritdoc} + */ + public function query() { + $active_index = array_search('_active', $this->value); + if ($active_index !== FALSE) { + $active_id = \Drupal::service('domain.negotiator')->getActiveId(); + $this->value[$active_index] = $active_id; + } + + parent::query(); + } +}