You are here

function _node_recur_node_recur_form in Node recur 7.2

Same name and namespace in other branches
  1. 7 node_recur.pages.inc \_node_recur_node_recur_form()

Helper function to provide the basics of the form

This helps make it available for other modules to use when we might not have a full node yet

Parameters

$type: The node type

2 calls to _node_recur_node_recur_form()
node_recur_node_recur_confirm in ./node_recur.pages.inc
Confirm form for node recur
node_recur_node_recur_form in ./node_recur.pages.inc
The node recur form

File

./node_recur.pages.inc, line 26

Code

function _node_recur_node_recur_form($node = NULL, &$form, &$form_state) {
  $field_name = node_recur_get_date_field_name($node->type);
  $field_inst = field_info_instance('node', $field_name, $node->type);
  $start_date = $node->{$field_name}[LANGUAGE_NONE][0]['value'];
  $field_view = field_view_field('node', $node, $field_name, $start_date);
  $lang = $field_view['#language'];

  // Add fieldset wrapper
  $form['node_recur'] = array(
    '#type' => 'fieldset',
    '#title' => t('Repeat'),
    '#weight' => isset($form[$field_name]) ? $form[$field_name]['#weight'] + 0.1 : 1,
  );
  $form['node_recur']['recur_date'] = array(
    '#type' => 'value',
    '#value' => $start_date,
  );
  $form['node_recur']['node_date_display'] = array(
    '#type' => 'item',
    '#title' => t('Start Date'),
    '#markup' => $field_view[0]['#markup'],
    '#weight' => -10,
  );

  // Get field data
  $form['#parents'] = [];

  // Less PHP Warnings
  $data = _field_invoke_default('form', 'node', $node, $form, $form_state, array(
    'field_id' => $field_inst['field_id'],
    'timezone' => 'UTC',
  ));

  // Faux set the field as repeatable
  $form_state['field'][$field_name][$lang]['field']['settings']['repeat'] = 1;

  // Add rrule field
  $form['node_recur'][$field_name] = $data[$field_name];
  $form['node_recur'][$field_name][$lang][0]['#theme'] = array(
    'date_textfield',
  );
  $form['node_recur'][$field_name][$lang][0]['#attributes']['id'] = $field_name;
  $form['node_recur'][$field_name][$lang][0]['#element_validate'][] = 'node_recur_node_recur_element_validate';
  $form['node_recur'][$field_name][$lang][0]['#element_validate'][] = 'date_repeat_field_widget_validate';
  $form['node_recur'][$field_name][$lang][0]['#date_is_default'] = TRUE;
  $form['node_recur'][$field_name][$lang][0]['show_repeat_settings'] = array(
    '#type' => 'hidden',
    '#value' => 1,
  );
  $form['node_recur'][$field_name][$lang][0]['rrule'] = array(
    '#type' => 'date_repeat_rrule',
    '#theme_wrappers' => array(
      'date_repeat_rrule',
    ),
    '#default_value' => '',
    '#date_timezone' => date_default_timezone(),
    '#date_label_position' => 'above',
    '#date_format' => 'm/d/Y - H:i',
    '#date_increment' => 1,
    '#date_year_range' => '-0:+1',
    '#date_repeat_widget' => $field_inst['widget']['type'],
    '#date_flexible' => 0,
    '#date_repeat_collapsed' => TRUE,
  );

  // Add missing weekend exclusion
  $form['node_recur'][$field_name]['exclude_weekends'] = array(
    '#type' => 'checkbox',
    '#title' => 'Exclude Weekends',
    '#default_value' => 1,
    '#return_value' => 1,
  );
  $form['#pre_render'][] = 'node_recur_pre_render';
  $form['submit'] = array(
    '#type' => 'submit',
    '#weight' => 99,
    '#value' => t('Generate'),
  );
  $form['#node'] = $node;
  $form['#entity_type'] = 'node';
  $form['#bundle'] = $node->type;

  // @kludge CSS to fix display the Date Repeat form
  // Some cosmetic fixes and a hacky, solution to hiding the faux field
  // Date Repeat Field can't see the show_repeat_settings option to display the form
  $form['#attached']['css'][] = drupal_get_path('module', 'node_recur') . '/node_recur.css';
  $form['#attached']['js'][] = drupal_get_path('module', 'node_recur') . '/node_recur.js';

  // Set a JS variable to handle defaults based on the original date
  $date_obj = new DateObject(is_array($start_date) ? $start_date['value'] : $start_date);
  $settings = $date_obj
    ->toArray();
  $settings['dow'] = strtolower(substr($date_obj
    ->format('D'), 0, 2));
  $settings['field_name'] = $field_name;
  $form['#attached']['js'][] = array(
    'data' => array(
      'node_recur' => $settings,
    ),
    'type' => 'setting',
  );
  return $form;
}