ShurlyLinkEdit.php in ShURLy 8
File
src/Plugin/views/field/ShurlyLinkEdit.php
View source
<?php
namespace Drupal\shurly\Plugin\views\field;
use Drupal\Core\Link;
use Drupal\Core\Url;
use Drupal\Core\Form\FormStateInterface;
use Drupal\views\Plugin\views\field\FieldPluginBase;
use Drupal\views\ResultRow;
use Drupal\views\ViewExecutable;
use Drupal\views\Plugin\views\display\DisplayPluginBase;
class ShurlyLinkEdit extends FieldPluginBase {
public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) {
parent::init($view, $display, $options);
$this->additional_fields['uid'] = 'uid';
$this->additional_fields['active'] = 'active';
$this->additional_fields['rid'] = 'rid';
}
protected function defineOptions() {
$options = parent::defineOptions();
$options['text'] = [
'default' => '',
'translatable' => TRUE,
];
return $options;
}
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
$form['text'] = [
'#type' => 'textfield',
'#title' => t('Text to display'),
'#default_value' => $this->options['text'],
];
parent::buildOptionsForm($form, $form_state);
}
public function query() {
$this
->ensureMyTable();
$this
->addAdditionalFields();
}
public function render(ResultRow $values) {
$user = \Drupal::currentUser();
$uid = $this
->getValue($values, 'uid');
$active = $this
->getValue($values, 'active');
if (!$active) {
return t('deactivated');
}
if (\Drupal::currentUser()
->hasPermission('Administer short URLs') || \Drupal::currentUser()
->hasPermission('Edit own URLs') && $uid == $user->uid) {
$text = !empty($this->options['text']) ? $this->options['text'] : t('edit');
$rid = $this
->getValue($values, 'rid');
return link::fromTextAndUrl($text, Url::fromUri('internal:/shurly/edit/' . $rid, [
'query' => \Drupal::service('redirect.destination')
->getAsArray(),
]))
->toString();
}
}
}