TransWikia.com

an error when changing the date form

Drupal Answers Asked by mahdi alikhasi on January 30, 2021

I need to change the date form of quiz module in quiz.module on line 1307
it was this:

  $form['quiz_availability']['quiz_open'] = array(
    '#type' => 'date',
    '#title' => t('Open Date'),
    '#default_value' => _quiz_form_prepare_date($node->quiz_open),
    '#description' => t('The date this @quiz will become available.', array('@quiz' => QUIZ_NAME)),
    '#after_build' => array('_quiz_limit_year_options'),
  );
  $form['quiz_availability']['quiz_close'] = array(
    '#type' => 'date',
    '#title' => t('Close Date'),
    '#default_value' => _quiz_form_prepare_date($node->quiz_close, variable_get('quiz_default_close', 30)),
    '#description' => t('The date this @quiz will cease to be available.', array('@quiz' => QUIZ_NAME)),
    '#after_build' => array('_quiz_limit_year_options'),
  );

and I changed it to this:

 $form['quiz_availability']['quiz_open'] = array(
    '#type' => 'date_select',
    '#date_format' => 'Y-m-d H:i',
    '#title' => t('Open Date'),
    '#default_value' => _quiz_form_prepare_date($node->quiz_open),
    '#description' => t('The date this @quiz will become available.', array('@quiz' => QUIZ_NAME)),
    '#after_build' => array('_quiz_limit_year_options'),
  );
  $form['quiz_availability']['quiz_close'] = array(
    '#type' => 'date_select',
    '#date_format' => 'Y-m-d H:i',
    '#title' => t('Close Date'),
    '#default_value' => _quiz_form_prepare_date($node->quiz_close, variable_get('quiz_default_close', 30)),
    '#description' => t('The date this @quiz will cease to be available.', array('@quiz' => QUIZ_NAME)),
    '#after_build' => array('_quiz_limit_year_options'),
  );

but now the webpage gives an error.
How do I fix this error?
The Error:

Please supply a valid date.
PDOException: SQLSTATE[01000]: Warning: 1265 Data truncated for column ‘quiz_open’ at row 1: INSERT INTO {quiz_node_properties} (vid,
nid, aid, number_of_random_questions, randomization,
backwards_navigation, repeat_until_correct, quiz_open, quiz_close,
takes, show_attempt_stats, keep_results, time_limit, pass_rate,
summary_pass, summary_pass_format, summary_default,
summary_default_format, quiz_always, feedback_time, display_feedback,
tid, has_userpoints, allow_skipping, allow_resume, allow_jumping,
show_passed, mark_doubtful) VALUES (:db_insert_placeholder_0,
:db_insert_placeholder_1, :db_insert_placeholder_2,
:db_insert_placeholder_3, :db_insert_placeholder_4,
:db_insert_placeholder_5, :db_insert_placeholder_6,
:db_insert_placeholder_7, :db_insert_placeholder_8,
:db_insert_placeholder_9, :db_insert_placeholder_10,
:db_insert_placeholder_11, :db_insert_placeholder_12,
:db_insert_placeholder_13, :db_insert_placeholder_14,
:db_insert_placeholder_15, :db_insert_placeholder_16,
:db_insert_placeholder_17, :db_insert_placeholder_18,
:db_insert_placeholder_19, :db_insert_placeholder_20,
:db_insert_placeholder_21, :db_insert_placeholder_22,
:db_insert_placeholder_23, :db_insert_placeholder_24,
:db_insert_placeholder_25, :db_insert_placeholder_26,
:db_insert_placeholder_27); Array ( [:db_insert_placeholder_0] => 24
[:db_insert_placeholder_1] => 22 [:db_insert_placeholder_2] => 0
[:db_insert_placeholder_3] => 0 [:db_insert_placeholder_4] => 2
[:db_insert_placeholder_5] => 1 [:db_insert_placeholder_6] => 0
[:db_insert_placeholder_7] => 2013-08-03 01:02
[:db_insert_placeholder_8] => 2013-09-02 15:13
[:db_insert_placeholder_9] => 1 [:db_insert_placeholder_10] => 1
[:db_insert_placeholder_11] => 2 [:db_insert_placeholder_12] => 0
[:db_insert_placeholder_13] => 75 [:db_insert_placeholder_14] =>
[:db_insert_placeholder_15] => plain_text [:db_insert_placeholder_16]
=> [:db_insert_placeholder_17] => plain_text [:db_insert_placeholder_18] => 1 [:db_insert_placeholder_19] => 1
[:db_insert_placeholder_20] => 1 [:db_insert_placeholder_21] => 0
[:db_insert_placeholder_22] => 0 [:db_insert_placeholder_23] => 1
[:db_insert_placeholder_24] => 1 [:db_insert_placeholder_25] => 1
[:db_insert_placeholder_26] => 1 [:db_insert_placeholder_27] => 0 ) in
quiz_insert() (in line 739
/home/…../public_html/…../sites/all/modules/quiz-7.x-4.0-beta1/quiz/quiz.module).

One Answer

When you want to override forms created by core or custom modules you should implement hook_form_alter() in a custom module.

It will look something like

/**
 * Implements hook_form_alter().
 */
function mymodule_form_alter(&$form, $form_state, $form_id)  {
  if ($form_id == 'quiz_form') {
    $form['quiz_availability']['quiz_open']['#type'] = 'date_select';
  }
}

Or as a hook_form_FORM_ID_alter() and then it would look like this

function mymodule_form_quiz_form_alter(&$form, &$form_state, $form_id) {
  $form['quiz_availability']['quiz_open']['#type'] = 'date_select';
}

Answered by Pontus Nilsson on January 30, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP