View source
<?php
namespace Drupal\ingredient\Plugin\Field\FieldWidget;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\WidgetBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\ingredient\Utility\IngredientUnitUtility;
use Drupal\ingredient\Utility\IngredientQuantityUtility;
use Symfony\Component\DependencyInjection\ContainerInterface;
class IngredientWidget extends WidgetBase {
protected $ingredientUnitUtility;
protected $ingredientQuantityUtility;
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, array $third_party_settings, IngredientUnitUtility $ingredient_unit_utility, IngredientQuantityUtility $ingredient_quantity_utility) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $third_party_settings);
$this->ingredientUnitUtility = $ingredient_unit_utility;
$this->ingredientQuantityUtility = $ingredient_quantity_utility;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($plugin_id, $plugin_definition, $configuration['field_definition'], $configuration['settings'], $configuration['third_party_settings'], $container
->get('ingredient.unit'), $container
->get('ingredient.quantity'));
}
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
$referenced_entities = $items
->referencedEntities();
$units = $this->ingredientUnitUtility
->getConfiguredUnits($this
->getFieldSetting('unit_sets'));
$units = $this->ingredientUnitUtility
->sortUnitsByName($units);
$quantity = isset($items[$delta]->quantity) ? preg_replace('/\\⁄/', '/', $this->ingredientQuantityUtility
->getQuantityFromDecimal($items[$delta]->quantity, '{%d} %d⁄%d', TRUE)) : '';
$element['quantity'] = [
'#type' => 'textfield',
'#title' => $this
->t('Quantity'),
'#default_value' => $quantity,
'#size' => 8,
'#maxlength' => 8,
'#attributes' => [
'class' => [
'recipe-ingredient-quantity',
],
],
];
$element['unit_key'] = [
'#type' => 'select',
'#title' => $this
->t('Unit'),
'#default_value' => isset($items[$delta]->unit_key) ? $items[$delta]->unit_key : $this
->getFieldSetting('default_unit'),
'#options' => $this->ingredientUnitUtility
->createUnitSelectOptions($units),
'#attributes' => [
'class' => [
'recipe-ingredient-unit-key',
],
],
];
$element['target_id'] = [
'#type' => 'entity_autocomplete',
'#title' => $this
->t('Name'),
'#target_type' => 'ingredient',
'#autocreate' => [
'bundle' => 'ingredient',
],
'#validate_reference' => FALSE,
'#default_value' => isset($referenced_entities[$delta]) ? $referenced_entities[$delta] : NULL,
'#size' => 25,
'#maxlength' => 128,
'#attributes' => [
'class' => [
'recipe-ingredient-name',
],
],
];
$element['note'] = [
'#type' => 'textfield',
'#title' => $this
->t('Note'),
'#default_value' => isset($items[$delta]->note) ? $items[$delta]->note : '',
'#size' => 40,
'#maxlength' => 255,
'#attributes' => [
'class' => [
'recipe-ingredient-note',
],
],
];
$element['#element_validate'] = [
[
$this,
'validate',
],
];
$element['#attached']['library'][] = 'ingredient/drupal.ingredient';
return $element;
}
public function validate(array $element, FormStateInterface $form_state) {
if (empty($element['unit_key']['#value']) && !empty($element['name']['#value'])) {
$form_state
->setError($element['unit_key'], $this
->t('You must choose a valid unit.'));
return;
}
}
public function massageFormValues(array $values, array $form, FormStateInterface $form_state) {
foreach ($values as $key => $value) {
$values[$key]['quantity'] = round($this->ingredientQuantityUtility
->getQuantityFromFraction($value['quantity']), 6);
if (is_array($value['target_id'])) {
unset($values[$key]['target_id']);
$values[$key] += $value['target_id'];
}
}
return $values;
}
}