HijriDefaultWidget.php in Hijri 3.0.x
File
src/Plugin/Field/FieldWidget/HijriDefaultWidget.php
View source
<?php
namespace Drupal\hijri\Plugin\Field\FieldWidget;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\WidgetBase;
use Drupal\Core\Form\FormStateInterface;
class HijriDefaultWidget extends WidgetBase {
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
$element['value'] = [
'#type' => 'date',
'#title' => $this
->t('Hijri'),
'#default_value' => isset($items[$delta]->value) ? $items[$delta]->value : NULL,
'#empty_value' => '',
'#placeholder' => $this
->t('Hijri'),
'#element_validate' => [
[
$this,
'validate',
],
],
];
$element['correction'] = [
'#type' => 'textfield',
'#title' => $this
->t('Correction'),
'#default_value' => isset($items[$delta]->correction) ? $items[$delta]->correction : NULL,
'#empty_value' => '',
'#placeholder' => $this
->t('Correction'),
];
return $element;
}
public function validate(&$element, FormStateInterface &$form_state, $form) {
$field_name = $element['#array_parents'][0];
$field_values = $form_state
->getValue([
$field_name,
]);
$value = $field_values[0]['value'];
if ($element['#required'] || strlen($value) > 0 || strlen($value) > 0) {
$form_state
->setError($element, $this
->t('Field is required.'));
}
}
}