ShipmentId.php in Ubercart 8.4
File
shipping/uc_fulfillment/src/Plugin/views/field/ShipmentId.php
View source
<?php
namespace Drupal\uc_fulfillment\Plugin\views\field;
use Drupal\Component\Utility\SafeMarkup;
use Drupal\Core\Form\FormStateInterface;
use Drupal\views\ResultRow;
use Drupal\views\ViewExecutable;
use Drupal\views\Plugin\views\display\DisplayPluginBase;
use Drupal\views\Plugin\views\field\FieldPluginBase;
class ShipmentId extends FieldPluginBase {
public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) {
parent::init($view, $display, $options);
if (!empty($this->options['link_to_shipment'])) {
$this->additional_fields['order_id'] = [
'table' => $this->table_alias,
'field' => 'order_id',
];
}
}
protected function defineOptions() {
$options = parent::defineOptions();
$options['link_to_shipment'] = [
'default' => FALSE,
];
return $options;
}
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
parent::buildOptionsForm($form, $form_state);
$form['link_to_shipment'] = [
'#title' => t('Link this field to the shipment page'),
'#description' => t('This will override any other link you have set.'),
'#type' => 'checkbox',
'#default_value' => !empty($this->options['link_to_shipment']),
];
}
protected function renderLink($data, $values) {
if (!empty($this->options['link_to_shipment'])) {
$this->options['alter']['make_link'] = FALSE;
if (\Drupal::currentUser()
->hasPermission('fulfill orders')) {
$path = 'admin/store/orders/' . $this
->get_value($values, 'order_id') . '/shipments/' . $values->{$this->field_alias};
}
else {
$path = FALSE;
}
if ($path && $data !== NULL && $data !== '') {
$this->options['alter']['make_link'] = TRUE;
$this->options['alter']['path'] = $path;
}
}
return $data;
}
public function render(ResultRow $values) {
return $this
->renderLink(SafeMarkup::checkPlain($values->{$this->field_alias}), $values);
}
}
Classes
Name |
Description |
ShipmentId |
Field handler: simple renderer that links to the shipment page. |