TransactionFormLinkWidget.php in Commerce Stock 8
File
modules/ui/src/Plugin/Field/FieldWidget/TransactionFormLinkWidget.php
View source
<?php
namespace Drupal\commerce_stock_ui\Plugin\Field\FieldWidget;
use Drupal\commerce\PurchasableEntityInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\WidgetBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Link;
class TransactionFormLinkWidget extends WidgetBase {
public function settingsSummary() {
$summary = [];
$summary[] = $this
->t('Provides a link to stock transaction form.');
return $summary;
}
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
$field = $items
->first();
$entity = $items
->getEntity();
if (!$entity instanceof PurchasableEntityInterface) {
return [];
}
if ($entity
->isNew()) {
return [];
}
$level = $field->available_stock;
$link = Link::createFromRoute($this
->t('transaction form'), 'commerce_stock_ui.stock_transactions2', [
'commerce_product_v_id' => $entity
->id(),
])
->toString();
$element['stock_level'] = [
'#type' => 'html_tag',
'#tag' => 'div',
'#value' => $this
->t('Current stock level: @stock_level', [
'@stock_level' => $level,
]),
];
$element['stock_transactions_form_link'] = [
'#type' => 'html_tag',
'#tag' => 'div',
'#value' => $this
->t('Please use the @transaction to create any stock transactions.', [
'@transaction' => $link,
]),
];
return $element;
}
}