DomainAccessField.php in Domain Access 8
File
domain_access/src/Plugin/views/field/DomainAccessField.php
View source
<?php
namespace Drupal\domain_access\Plugin\views\field;
use Drupal\views\ResultRow;
use Drupal\views\Plugin\views\field\EntityField;
class DomainAccessField extends EntityField {
public function getItems(ResultRow $values) {
$items = parent::getItems($values);
if (!empty($this->options['settings']['link'])) {
foreach ($items as &$item) {
$object = $item['raw'];
$entity = $object
->getEntity();
$url = $entity
->toUrl('canonical', [
'external' => TRUE,
])
->toString();
$domain = $item['rendered']['#options']['entity'];
$item['rendered']['#type'] = 'markup';
$item['rendered']['#markup'] = '<a href="' . $domain
->buildUrl($url) . '">' . $domain
->label() . '</a>';
}
uasort($items, [
$this,
'sort',
]);
}
return $items;
}
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() ? 1 : 0;
}
return strcmp($a['rendered']['#plain_text'], $b['rendered']['#plain_text']);
}
}