From a0fb5ddc6f10cca8bfc64863081dd628e2c5d571 Mon Sep 17 00:00:00 2001 From: astanul Date: Mon, 12 Mar 2018 17:49:56 +0300 Subject: [PATCH 1/2] The code for Duration Display (for times with End Time) --- .../TimeFieldDefaultFormatter.php | 5 + src/Plugin/Field/FieldType/TimeField.php | 12 +- src/Plugin/Field/FieldType/TimeField.php.bak | 53 ++++ .../FieldWidget/TimeFieldStandardWidget.php | 21 ++ .../TimeFieldStandardWidget.php.bak | 279 ++++++++++++++++++ 5 files changed, 368 insertions(+), 2 deletions(-) create mode 100644 src/Plugin/Field/FieldType/TimeField.php.bak create mode 100644 src/Plugin/Field/FieldWidget/TimeFieldStandardWidget.php.bak diff --git a/src/Plugin/Field/FieldFormatter/TimeFieldDefaultFormatter.php b/src/Plugin/Field/FieldFormatter/TimeFieldDefaultFormatter.php index b975416..b73e6d7 100644 --- a/src/Plugin/Field/FieldFormatter/TimeFieldDefaultFormatter.php +++ b/src/Plugin/Field/FieldFormatter/TimeFieldDefaultFormatter.php @@ -61,6 +61,11 @@ public function viewElements(FieldItemListInterface $items, $langcode) { if (!empty($item->value)) { $element[$delta]['#markup'] = trim(timefield_integer_to_time($settings['display_format'], $item->value)); } + if (!empty($item->value2)) { + $element[$delta]['#markup'] = (!empty($item->value)) ? + $element[$delta]['#markup'].'-'.trim(timefield_integer_to_time($settings['display_format'], $item->value2)) : + trim(timefield_integer_to_time($settings['display_format'], $item->value2)); + } } diff --git a/src/Plugin/Field/FieldType/TimeField.php b/src/Plugin/Field/FieldType/TimeField.php index 294d4d7..80d5626 100644 --- a/src/Plugin/Field/FieldType/TimeField.php +++ b/src/Plugin/Field/FieldType/TimeField.php @@ -30,6 +30,11 @@ public static function schema(FieldStorageDefinitionInterface $field_definition) 'not null' => FALSE, 'default' => NULL, ), + 'value2' => array( + 'type' => 'int', + 'not null' => FALSE, + 'default' => NULL, + ), ), ); } @@ -39,7 +44,8 @@ public static function schema(FieldStorageDefinitionInterface $field_definition) */ public function isEmpty() { $value = $this->get('value')->getValue(); - return $value === NULL || $value === ''; + $value2 = $this->get('value2')->getValue(); + return ($value === NULL || $value === '') && ($value2 === NULL || $value2 === ''); } /** @@ -47,7 +53,9 @@ public function isEmpty() { */ public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) { $properties['value'] = DataDefinition::create('integer') - ->setLabel(t('Timefield')); + ->setLabel(t('Timefield Begin')); + $properties['value2'] = DataDefinition::create('integer') + ->setLabel(t('Timefield End')); return $properties; } } diff --git a/src/Plugin/Field/FieldType/TimeField.php.bak b/src/Plugin/Field/FieldType/TimeField.php.bak new file mode 100644 index 0000000..294d4d7 --- /dev/null +++ b/src/Plugin/Field/FieldType/TimeField.php.bak @@ -0,0 +1,53 @@ + array( + 'value' => array( + 'type' => 'int', + 'not null' => FALSE, + 'default' => NULL, + ), + ), + ); + } + + /** + * {@inheritdoc} + */ + public function isEmpty() { + $value = $this->get('value')->getValue(); + return $value === NULL || $value === ''; + } + + /** + * {@inheritdoc} + */ + public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) { + $properties['value'] = DataDefinition::create('integer') + ->setLabel(t('Timefield')); + return $properties; + } +} diff --git a/src/Plugin/Field/FieldWidget/TimeFieldStandardWidget.php b/src/Plugin/Field/FieldWidget/TimeFieldStandardWidget.php index b80dadf..ed51413 100644 --- a/src/Plugin/Field/FieldWidget/TimeFieldStandardWidget.php +++ b/src/Plugin/Field/FieldWidget/TimeFieldStandardWidget.php @@ -68,9 +68,13 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen ); $value = isset($items[$delta]) ? timefield_integer_to_time($instance_settings, $items[$delta]->value) : ''; + + $value2 = isset($items[$delta]) ? timefield_integer_to_time($instance_settings, $items[$delta]->value2) : ''; + $element['value'] = array( '#type' => 'textfield', '#title' => \Drupal\Component\Utility\Xss::filter($element['#title']), + '#title' => t('The begin date'), '#description' => \Drupal\Component\Utility\Xss::filter($element['#description']), '#default_value' => $value, '#required' => $element['#required'], @@ -84,6 +88,23 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen ) ), ); + $element['value2'] = array( + '#type' => 'textfield', + //'#title' => \Drupal\Component\Utility\Xss::filter($element['#title']), + '#title' => t('The end date'), + '#description' => t('In the case of an empty value, the duration will not be displayed: instead Start Time - End Time of the value, we\'ll see Single Time Value.'), + '#default_value' => $value2, + '#required' => $element['#required'], + '#weight' => (isset($element['#weight'])) ? $element['#weight'] : 0, + '#delta' => $delta, + '#element_validate' => array('timefield_time_validate'), + '#attributes' => array( + 'class' => array( + 'edit-timefield-timepicker', + $instance_class + ) + ), + ); return $element; } diff --git a/src/Plugin/Field/FieldWidget/TimeFieldStandardWidget.php.bak b/src/Plugin/Field/FieldWidget/TimeFieldStandardWidget.php.bak new file mode 100644 index 0000000..b80dadf --- /dev/null +++ b/src/Plugin/Field/FieldWidget/TimeFieldStandardWidget.php.bak @@ -0,0 +1,279 @@ +getLibraryByName('timefield', 'timepicker'); + return array( + 'disable_plugin' => empty($library) ? TRUE : FALSE, + 'separator' => ':', + 'showLeadingZero' => FALSE, + 'showPeriod' => FALSE, + 'periodSeparator' => '', + 'am_text' => 'AM', + 'pm_text' => 'PM', + 'showCloseButton' => FALSE, + 'closeButtonText' => 'Close', + 'showNowButton' => FALSE, + 'nowButtonText' => 'Now', + 'showDeselectButton' => FALSE, + 'deselectButtonText' => 'Deselect', + 'myPosition' => 'left top', + 'atPosition' => 'left bottom', + + ) + parent::defaultSettings(); + } + + /** + * {@inheritdoc} + */ + public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) { + $instance_class = str_replace('_', '-', $items->getName()) . "-" . $delta; + $instance_settings = $this->getSettings(); + if (!$instance_settings['disable_plugin']) { + $js_settings = _timefield_js_settings($instance_class, $instance_settings); + $context = array( + 'type' => 'field', + 'items' => $items, + ); + + \Drupal::moduleHandler()->alter('timefield_js_settings', $js_settings, $context); + + $element['#attached']['library'][] = 'timefield/timepicker'; + $element['#attached']['library'][] = 'timefield/timefield'; + $element['#attached']['drupalSettings']['timefield'][$instance_class] = $js_settings; + } + + $element += array( + '#delta' => $delta, + ); + + $value = isset($items[$delta]) ? timefield_integer_to_time($instance_settings, $items[$delta]->value) : ''; + $element['value'] = array( + '#type' => 'textfield', + '#title' => \Drupal\Component\Utility\Xss::filter($element['#title']), + '#description' => \Drupal\Component\Utility\Xss::filter($element['#description']), + '#default_value' => $value, + '#required' => $element['#required'], + '#weight' => (isset($element['#weight'])) ? $element['#weight'] : 0, + '#delta' => $delta, + '#element_validate' => array('timefield_time_validate'), + '#attributes' => array( + 'class' => array( + 'edit-timefield-timepicker', + $instance_class + ) + ), + ); + + return $element; + } + + public function settingsForm(array $form, FormStateInterface $form_state) { + $elements = parent::settingsForm($form, $form_state); + + $library = \Drupal::service('library.discovery') + ->getLibraryByName('timefield', 'timepicker'); + if (empty($library)) { + drupal_set_message("You will not have enhanced time input widget without downloading the plugin. " . \Drupal::l("Read installation instructions here.", \Drupal\Core\Url::fromUri('http://drupalcode.org/project/timefield.git/blob_plain/HEAD:/README.txt')), 'warning'); + } + $elements['disable_plugin'] = array( + '#title' => t('Disable jQuery Timepicker plugin.'), + '#type' => 'checkbox', + '#default_value' => $this->getSetting('disable_plugin'), + '#description' => t('Do not use jQuery Timepicker plugin for input.'), + '#disabled' => (empty($library)), + ); + + $elements['input_format'] = array( + '#title' => t('Time Input Format'), + '#type' => 'fieldset', + ); + $elements['input_format']['separator'] = array( + '#title' => t('Hour and Minute Separator'), + '#type' => 'textfield', + '#default_value' => $this->getSetting('separator'), + '#size' => 10, + '#description' => t('The character to use to separate hours and minutes.'), + ); + $elements['input_format']['showLeadingZero'] = array( + '#title' => t('Show Leading Zero for Hour'), + '#type' => 'checkbox', + '#default_value' => $this->getSetting('showLeadingZero'), + '#description' => t('Whether or not to show a leading zero for hours < 10.'), + ); + $elements['input_format']['showPeriod'] = array( + '#title' => t('Show AM/PM Label'), + '#type' => 'checkbox', + '#default_value' => $this->getSetting('showPeriod'), + '#description' => t('Whether or not to show AM/PM on the input textfield both on the widget and in the text field after selecting the time with the widget.'), + ); + $elements['input_format']['periodSeparator'] = array( + '#title' => t('What character should appear between the time and the Period (AM/PM)'), + '#type' => 'textfield', + '#default_value' => $this->getSetting('periodSeparator'), + '#size' => 10, + '#description' => t('The character to use to separate the time from the time period (AM/PM).'), + ); + $elements['input_format']['am_text'] = array( + '#title' => t('AM text'), + '#type' => 'textfield', + '#default_value' => $this->getSetting('am_text'), + '#size' => 10, + ); + $elements['input_format']['pm_text'] = array( + '#title' => t('PM text'), + '#type' => 'textfield', + '#default_value' => $this->getSetting('pm_text'), + '#size' => 10, + ); + $elements['input_format']['showCloseButton'] = array( + '#title' => t('Show a Button to Close the Picker Widget'), + '#type' => 'checkbox', + '#default_value' => $this->getSetting('showCloseButton'), + '#states' => array( + 'invisible' => array( + ':input[name="fields[field_ido][settings_edit_form][settings][disable_plugin]"]' => array('checked' => TRUE), + ), + ), + ); + $elements['input_format']['closeButtonText'] = array( + '#title' => t('Close Button text'), + '#type' => 'textfield', + '#default_value' => $this->getSetting('closeButtonText'), + '#size' => 10, + '#states' => array( + 'invisible' => array( + ':input[name="fields[field_ido][settings_edit_form][settings][disable_plugin]"]' => array('checked' => TRUE), + ), + ), + ); + $elements['input_format']['showNowButton'] = array( + '#title' => t('Show a Button to Select the Current Time'), + '#type' => 'checkbox', + '#default_value' => $this->getSetting('showNowButton'), + '#states' => array( + 'invisible' => array( + ':input[name="fields[field_ido][settings_edit_form][settings][disable_plugin]"]' => array('checked' => TRUE), + ), + ), + ); + $elements['input_format']['nowButtonText'] = array( + '#title' => t('Now Button text'), + '#type' => 'textfield', + '#default_value' => $this->getSetting('nowButtonText'), + '#size' => 10, + '#states' => array( + 'invisible' => array( + ':input[name="fields[field_ido][settings_edit_form][settings][disable_plugin]"]' => array('checked' => TRUE), + ), + ), + ); + $elements['input_format']['showDeselectButton'] = array( + '#title' => t('Show a Button to Deselect the time in the Picker Widget'), + '#type' => 'checkbox', + '#default_value' => $this->getSetting('showDeselectButton'), + '#states' => array( + 'invisible' => array( + ':input[name="fields[field_ido][settings_edit_form][settings][disable_plugin]"]' => array('checked' => TRUE), + ), + ), + ); + $elements['input_format']['deselectButtonText'] = array( + '#title' => t('Deselect Button text'), + '#type' => 'textfield', + '#default_value' => $this->getSetting('deselectButtonText'), + '#size' => 10, + '#states' => array( + 'invisible' => array( + ':input[name="fields[field_ido][settings_edit_form][settings][disable_plugin]"]' => array('checked' => TRUE), + ), + ), + ); + $elements['input_format']['myPosition'] = array( + '#title' => t('my Position'), + '#type' => 'select', + '#default_value' => $this->getSetting('myPosition'), + '#options' => array_combine(array( + 'left top', + 'left center', + 'left bottom', + 'center top', + 'center center', + 'center bottom', + 'right top', + 'right center', + 'right bottom' + ), array( + 'left top', + 'left center', + 'left bottom', + 'center top', + 'center center', + 'center bottom', + 'right top', + 'right center', + 'right bottom' + )), + '#description' => t('Corner of the timpicker widget dialog to position. See !jquery_info for more info.', array('!jquery_info' => \Drupal::l(t("jQuery UI Position documentation"), \Drupal\Core\Url::fromUri('http://jqueryui.com/demos/position')))), + '#states' => array( + 'invisible' => array( + ':input[name="fields[field_ido][settings_edit_form][settings][disable_plugin]"]' => array('checked' => TRUE), + ), + ), + ); + $elements['input_format']['atPosition'] = array( + '#title' => t('at Position'), + '#type' => 'select', + '#options' => array_combine(array( + 'left top', + 'left center', + 'left bottom', + 'center top', + 'center center', + 'center bottom', + 'right top', + 'right center', + 'right bottom' + ), array( + 'left top', + 'left center', + 'left bottom', + 'center top', + 'center center', + 'center bottom', + 'right top', + 'right center', + 'right bottom' + )), + '#default_value' => $this->getSetting('atPosition'), + '#description' => t('Where to position "my Position" relative to input widget textfield See !jquery_info for more info.', array('!jquery_info' => \Drupal::l(t("jQuery UI Position documentation"), \Drupal\Core\Url::fromUri('http://jqueryui.com/demos/position')))), + '#states' => array( + 'invisible' => array( + ':input[name="fields[field_ido][settings_edit_form][settings][disable_plugin]"]' => array('checked' => TRUE), + ), + ), + ); + + return $elements; + } +} From e878bab9b46cc1a34b90f610782168268522b331 Mon Sep 17 00:00:00 2001 From: astanul Date: Mon, 12 Mar 2018 18:00:19 +0300 Subject: [PATCH 2/2] The code for Duration Display --- .../TimeFieldDefaultFormatter.php | 5 ----- src/Plugin/Field/FieldType/TimeField.php | 12 ++--------- .../FieldWidget/TimeFieldStandardWidget.php | 21 ------------------- 3 files changed, 2 insertions(+), 36 deletions(-) diff --git a/src/Plugin/Field/FieldFormatter/TimeFieldDefaultFormatter.php b/src/Plugin/Field/FieldFormatter/TimeFieldDefaultFormatter.php index b73e6d7..b975416 100644 --- a/src/Plugin/Field/FieldFormatter/TimeFieldDefaultFormatter.php +++ b/src/Plugin/Field/FieldFormatter/TimeFieldDefaultFormatter.php @@ -61,11 +61,6 @@ public function viewElements(FieldItemListInterface $items, $langcode) { if (!empty($item->value)) { $element[$delta]['#markup'] = trim(timefield_integer_to_time($settings['display_format'], $item->value)); } - if (!empty($item->value2)) { - $element[$delta]['#markup'] = (!empty($item->value)) ? - $element[$delta]['#markup'].'-'.trim(timefield_integer_to_time($settings['display_format'], $item->value2)) : - trim(timefield_integer_to_time($settings['display_format'], $item->value2)); - } } diff --git a/src/Plugin/Field/FieldType/TimeField.php b/src/Plugin/Field/FieldType/TimeField.php index 80d5626..294d4d7 100644 --- a/src/Plugin/Field/FieldType/TimeField.php +++ b/src/Plugin/Field/FieldType/TimeField.php @@ -30,11 +30,6 @@ public static function schema(FieldStorageDefinitionInterface $field_definition) 'not null' => FALSE, 'default' => NULL, ), - 'value2' => array( - 'type' => 'int', - 'not null' => FALSE, - 'default' => NULL, - ), ), ); } @@ -44,8 +39,7 @@ public static function schema(FieldStorageDefinitionInterface $field_definition) */ public function isEmpty() { $value = $this->get('value')->getValue(); - $value2 = $this->get('value2')->getValue(); - return ($value === NULL || $value === '') && ($value2 === NULL || $value2 === ''); + return $value === NULL || $value === ''; } /** @@ -53,9 +47,7 @@ public function isEmpty() { */ public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) { $properties['value'] = DataDefinition::create('integer') - ->setLabel(t('Timefield Begin')); - $properties['value2'] = DataDefinition::create('integer') - ->setLabel(t('Timefield End')); + ->setLabel(t('Timefield')); return $properties; } } diff --git a/src/Plugin/Field/FieldWidget/TimeFieldStandardWidget.php b/src/Plugin/Field/FieldWidget/TimeFieldStandardWidget.php index ed51413..b80dadf 100644 --- a/src/Plugin/Field/FieldWidget/TimeFieldStandardWidget.php +++ b/src/Plugin/Field/FieldWidget/TimeFieldStandardWidget.php @@ -68,13 +68,9 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen ); $value = isset($items[$delta]) ? timefield_integer_to_time($instance_settings, $items[$delta]->value) : ''; - - $value2 = isset($items[$delta]) ? timefield_integer_to_time($instance_settings, $items[$delta]->value2) : ''; - $element['value'] = array( '#type' => 'textfield', '#title' => \Drupal\Component\Utility\Xss::filter($element['#title']), - '#title' => t('The begin date'), '#description' => \Drupal\Component\Utility\Xss::filter($element['#description']), '#default_value' => $value, '#required' => $element['#required'], @@ -88,23 +84,6 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen ) ), ); - $element['value2'] = array( - '#type' => 'textfield', - //'#title' => \Drupal\Component\Utility\Xss::filter($element['#title']), - '#title' => t('The end date'), - '#description' => t('In the case of an empty value, the duration will not be displayed: instead Start Time - End Time of the value, we\'ll see Single Time Value.'), - '#default_value' => $value2, - '#required' => $element['#required'], - '#weight' => (isset($element['#weight'])) ? $element['#weight'] : 0, - '#delta' => $delta, - '#element_validate' => array('timefield_time_validate'), - '#attributes' => array( - 'class' => array( - 'edit-timefield-timepicker', - $instance_class - ) - ), - ); return $element; }