MaestroEngineEntityIdentifierEntityLabel.php in Maestro 8.2
File
src/Plugin/views/field/MaestroEngineEntityIdentifierEntityLabel.php
View source
<?php
namespace Drupal\maestro\Plugin\views\field;
use Drupal\Core\Form\FormStateInterface;
use Drupal\views\Plugin\views\field\FieldPluginBase;
use Drupal\views\ResultRow;
class MaestroEngineEntityIdentifierEntityLabel extends FieldPluginBase {
public function query() {
}
protected function defineOptions() {
$options = parent::defineOptions();
$options['link_to_entity'] = [
'default' => 0,
];
return $options;
}
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
$form['link_to_entity'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Provide a link to the entity?'),
'#description' => $this
->t('When checked, the output in the view will show a link to the entity.'),
'#default_value' => isset($this->options['link_to_entity']) ? $this->options['link_to_entity'] : 0,
];
parent::buildOptionsForm($form, $form_state);
}
public function render(ResultRow $values) {
$result = '';
$urlToEntity = '';
$item = $values->_entity;
if ($item
->getEntityTypeId() == 'maestro_entity_identifiers') {
$entity_manager = \Drupal::entityTypeManager();
$entity = $entity_manager
->getStorage($item->entity_type
->getString())
->load($item->entity_id
->getString());
if (isset($entity)) {
$result = $entity
->label();
$urlToEntity = $entity
->access('view') ? $entity
->toUrl('canonical', [
'query' => [
'maestro' => 1,
],
])
->toString() : '';
}
else {
$result = '';
}
}
else {
return '';
}
if ($this->options['link_to_entity'] && $result && $urlToEntity) {
return [
'#markup' => '<a href="' . $urlToEntity . '" class="maestro_who_completed_field">' . $result . '</a>',
];
}
else {
return $result;
}
}
}