Skip to content
Snippets Groups Projects
Commit a66c15d3 authored by Bohdan Artemchuk's avatar Bohdan Artemchuk
Browse files

Issue #3339263 by bohart: Added saving of the parcel locker name to the field.

parent e5c9ac7c
No related branches found
No related tags found
No related merge requests found
......@@ -11,8 +11,8 @@ use Drupal\Core\Render\Element\FormElement;
*
* Usage example:
* @code
* $form['loko_parcel'] = [
* '#type' => 'loko_parcel',
* $form['loko_locker'] = [
* '#type' => 'loko_locker',
* '#default_value' => [
* 'parcel_locker_id' => 'a9522a9d-eaf5-11e7-ba66-005056b2fc3d',
* 'parcel_locker_name' => 'Test parcel locker',
......@@ -20,14 +20,14 @@ use Drupal\Core\Render\Element\FormElement;
* ];
* @endcode
*
* @FormElement("loko_parcel")
* @FormElement("loko_locker")
*/
class LokoElement extends FormElement {
/**
* {@inheritdoc}
*/
public function getInfo() {
public function getInfo(): array {
return [
'#input' => TRUE,
'#multiple' => FALSE,
......@@ -35,6 +35,9 @@ class LokoElement extends FormElement {
'#process' => [
[get_class($this), 'processElement'],
],
'#element_validate' => [
[get_class($this), 'validateLockerName'],
],
'#theme_wrappers' => ['container'],
];
}
......@@ -48,7 +51,7 @@ class LokoElement extends FormElement {
* @return array
* The modified value.
*/
public static function applyDefaults(array $value) {
public static function applyDefaults(array $value): array {
$properties = [
'parcel_locker_id',
'parcel_locker_name',
......@@ -70,6 +73,7 @@ class LokoElement extends FormElement {
if (is_array($input)) {
$input = static::applyDefaults($input);
}
$element['#default_value'] = (array) $element['#default_value'];
$element['#default_value'] = static::applyDefaults($element['#default_value']);
......@@ -120,4 +124,13 @@ class LokoElement extends FormElement {
return $element;
}
/**
* Validate parcel locker ID to save parcel locker name.
*/
public static function validateLockerName(array &$element, FormStateInterface $form_state, array &$complete_form) {
$locker_id = $form_state->getValue($element['#parents'])['parcel_locker_id'];
$locker_name = $element['parcel_locker_id']['#options'][$locker_id];
$form_state->setValue(array_merge($element['#parents'], ['parcel_locker_name']), $locker_name);
}
}
......@@ -88,7 +88,7 @@ interface LokoClientInterface {
// public function createReservation(string $options): ?array;
/**
* Method for creating parcel.
* Method for creating a new parcel.
*
* @param string $options
* The model.
......@@ -101,13 +101,13 @@ interface LokoClientInterface {
/**
* Method for confirming the reservation.
*
* @param string $parcel_locker_id
* @param string $parcel_id
* The store identifier.
*
* @return bool
* TRUE if reservation is confirmed.
*/
// public function confirmReservation(string $parcel_locker_id): bool;
// public function confirmReservation(string $parcel_id): bool;
// @codingStandardsIgnoreEnd
}
......@@ -12,10 +12,10 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
* Plugin implementation of the Loko formatter.
*
* @FieldFormatter(
* id = "loko_parcel_formatter",
* label = @Translation("Loko Parcel"),
* id = "loko_locker_formatter",
* label = @Translation("Loko Parcel Locker"),
* field_types = {
* "loko_parcel"
* "loko_locker"
* }
* )
*/
......@@ -75,17 +75,16 @@ class LokoFormatter extends FormatterBase {
public function viewElements(FieldItemListInterface $items, $langcode): array {
$elements = [];
foreach ($items as $delta => $item) {
$elements[$delta]['parcel_locker_id'] = [
$elements[$delta]['parcel_locker_name'] = [
'#type' => 'html_tag',
'#tag' => 'div',
'#value' => $item->parcel_locker_id,
'#value' => $item->parcel_locker_name,
'#attributes' => [
'class' => ['parcel-locker-id'],
'data-locker-id' => $item->parcel_locker_id,
],
];
}
// @todo Load about details about parcel locker from API.
// @todo Show parcel locker name instead of id.
return $elements;
}
......
......@@ -12,12 +12,12 @@ use Drupal\Core\TypedData\DataDefinition;
* Plugin implementation of the Loko field type.
*
* @FieldType(
* id = "loko_parcel",
* id = "loko_locker",
* label = @Translation("Loko Parcel Locker"),
* category = @Translation("Address"),
* description = @Translation("Field containing Loko parcel locker."),
* default_widget = "loko_parcel_widget",
* default_formatter = "loko_parcel_formatter"
* default_widget = "loko_locker_widget",
* default_formatter = "loko_locker_formatter"
* )
*/
class LokoItem extends FieldItemBase {
......
......@@ -13,11 +13,11 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
* Plugin implementation of the Loko widget.
*
* @FieldWidget(
* id = "loko_parcel_widget",
* id = "loko_locker_widget",
* module = "commerce_loko",
* label = @Translation("Loko Parcel"),
* field_types = {
* "loko_parcel"
* "loko_locker"
* }
* )
*/
......@@ -77,7 +77,7 @@ class LokoWidget extends WidgetBase {
}
return [
'#type' => 'loko_parcel',
'#type' => 'loko_locker',
'#shipment_method' => $this->getFieldSettings()['shipping_method'],
'#default_value' => $items[$delta]->getValue(),
] + $element;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment