CustomContextualLinkListBuilder.php in Custom Contextual Links 8.2
File
src/CustomContextualLinkListBuilder.php
View source
<?php
namespace Drupal\ccl;
use Drupal\Core\Config\Entity\ConfigEntityListBuilder;
use Drupal\Core\Entity\EntityInterface;
use Drupal\node\Entity\Node;
class CustomContextualLinkListBuilder extends ConfigEntityListBuilder {
public function buildHeader() {
$header['label'] = $this
->t('Label');
$header['title'] = $this
->t('Link Title');
$header['url'] = $this
->t('URL');
$header['type'] = $this
->t('Type');
$header['options'] = $this
->t('Options');
return $header + parent::buildHeader();
}
public function buildRow(EntityInterface $entity) {
$row['label'] = $entity
->label();
$row['title'] = $entity
->get('title');
$row['link'] = $entity
->get('link');
$row['type'] = $entity
->get('type');
$row['options'] = '';
if ($entity
->get('type') == 'node') {
$cts = node_type_get_names();
switch ($entity
->getLinkOption('node_option')) {
case 'global':
$row['options'] = $this
->t('Attached to all nodes.');
break;
case 'ct':
$row['options'] = $this
->t('Attached to all nodes of the content type %ct.', [
'%ct' => $cts[$entity
->getLinkOption('node_type')],
]);
break;
case 'node':
$nid = $entity
->getLinkOption('node_id');
$node = $nid ? Node::load($nid) : NULL;
if ($node) {
$row['options'] = $this
->t('Attached to %node_title [NID: :nid].', [
'%node_title' => $node
->label(),
':nid' => $node
->id(),
]);
}
break;
}
}
else {
foreach ($this
->moduleHandler()
->getImplementations('ccl_link_info') as $implementation) {
$options = $this
->moduleHandler()
->invoke($implementation, 'ccl_link_info', [
$entity,
]);
if (!empty($options)) {
$row['options'] = $options;
break;
}
}
}
return $row + parent::buildRow($entity);
}
}