Last active
June 22, 2017 21:17
-
-
Save jhedstrom/a96424a072b3fbed216821480307c5f9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Drupal\my_module\Form; | |
use Drupal\Core\Datetime\Element\Datetime; | |
use Drupal\Core\Form\FormStateInterface; | |
/** | |
* Utility functions and static callbacks for MY MODULE module. | |
*/ | |
class FormHelper { | |
/** | |
* Value callback to set default time when time is optional. | |
* | |
* @param array $element | |
* The form element. | |
* @param array $input | |
* An input array (contains `date` and `time` components. | |
* @param \Drupal\Core\Form\FormStateInterface $form_state | |
* The form state object. | |
* | |
* @return array | |
* The value array. | |
* | |
* @see \Drupal\Core\Datetime\Element\Datetime::valueCallback | |
*/ | |
public static function setDefaultTime(&$element, $input, FormStateInterface $form_state) { | |
if ($input['date'] && empty($input['time'])) { | |
$input['time'] = '00:00:00'; | |
} | |
return Datetime::valueCallback($element, $input, $form_state); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Implements hook_field_widget_WIDGET_TYPE_form_alter(). | |
*/ | |
function my_module_field_widget_datetime_default_form_alter(&$element, FormStateInterface $form_state, $context) { | |
/** @var \Drupal\Core\Field\WidgetInterface $widget */ | |
$widget = $context['widget']; | |
if ($widget->getThirdPartySetting('my_module', 'time_optional', FALSE)) { | |
// Time part of the date field is optional, and will default to 00:00. | |
// Set a callback function to alter the actual form widget. | |
$element['value']['#date_time_callbacks'][] = 'my_module_field_widget_time_optional'; | |
$element['value']['#value_callback'] = [FormHelper::class, 'setDefaultTime']; | |
} | |
} | |
/** | |
* Implements hook_field_widget_third_party_settings_form(). | |
*/ | |
function my_module_field_widget_third_party_settings_form(WidgetInterface $plugin, FieldDefinitionInterface $field_definition, $form_mode, $form, FormStateInterface $form_state) { | |
$element = []; | |
if ($plugin->getPluginId() == 'datetime_default') { | |
$element['time_optional'] = [ | |
'#type' => 'checkbox', | |
'#title' => t('Time is optional'), | |
'#description' => t('If selected, entering the time part of the date is optional and will default to 00:00:00.'), | |
'#default_value' => $plugin->getThirdPartySetting('my_module', 'time_optional', FALSE), | |
]; | |
} | |
return $element; | |
} | |
/** | |
* Implements hook_field_widget_settings_summary_alter(). | |
*/ | |
function my_module_field_widget_settings_summary_alter(&$summary, $context) { | |
if ($context['widget']->getPluginId() == 'datetime_default') { | |
if ($context['widget']->getThirdPartySetting('my_module', 'time_optional', FALSE)) { | |
$summary[] = t('Time is optional'); | |
} | |
} | |
} | |
/** | |
* Makes the time portion of a datetime form element optional. | |
* | |
* Note, this isn't in FormHelper since the Datetime element uses a call to | |
* `function_exists` rather than `is_callable`. | |
* | |
* @param array $element | |
* The form element. | |
* @param \Drupal\Core\Form\FormStateInterface $form_state | |
* The form state object. | |
* @param mixed $date | |
* The date object or array. | |
*/ | |
function my_module_field_widget_time_optional(&$element, FormStateInterface $form_state, $date) { | |
$element['time']['#required'] = FALSE; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment