You are here

function _office_hours_block_process in Office Hours 7

Process an individual element.

Build the form element. When creating a form using FAPI #process, note that $element['#value'] is already set.

1 string reference to '_office_hours_block_process'
office_hours_element_info in ./office_hours.module
Implements FAPI hook_element_info().

File

includes/office_hours.elements.inc, line 16
Office hours form elements and their theming and validation.

Code

function _office_hours_block_process($element, &$form_state, $form) {
  $element['#prefix'] = '';
  $element['#suffix'] = '';
  $has_comment = isset($element['#field_settings']['comment']) ? $element['#field_settings']['comment'] : FALSE;
  $blocks_per_day = $element['#field_settings']['cardinality'];
  $daydelta = $element['#daydelta'];
  $day = $element['#day'];
  $same_link = '';
  if ($daydelta == 0) {

    // This is the first block of the day, show the dayname.
    $label = t($element['#dayname']);
    $label_style = '';
    $block_style = '';

    // Add 'Same as above' link, also for first day in widget, which copies from last day.
    $same_link_text = t($day != $element['#field_settings']['date_first_day'] ? 'Same as above' : 'Same as last day');
    $same_link = l($same_link_text, '', array(
      'attributes' => array(
        'class' => 'oh-same-link',
      ),
    ));
  }
  elseif ($daydelta >= $blocks_per_day) {

    // In case the number of blocks per day was lowered by admin, this element
    // may have a value. Better clear it (in case a value was entered before).
    // The value will be removed upon the next 'Save' action.
    $label = '';
    $label_style = '';

    // The following style is only needed if js isn't working.
    $block_style = 'style = "display:none;"';
    if (empty($element['#value'])) {
      $element['#value'] = array();
    }
    $element['#value']['starthours'] = '';
    $element['#value']['endhours'] = '';

    // The following class is the trigger for js to hide the row.
    $element['#prefix'] .= "<div class='oh-hide'>";
    $element['#suffix'] .= '</div>';
  }
  elseif (!empty($element['#value']['starthours'])) {

    // This is a following block with contents, so we show the times.
    $label = t('and');
    $label_style = 'style = "text-align:right;"';
    $block_style = '';
  }
  else {

    // This is an empty following block, so we show the 'add hours' js-link.
    $label = t('and');
    $label_style = 'style = "text-align:right;"';
    $block_style = 'style = "display:none;"';
    $link = l(t('Add new @node_type', array(
      '@node_type' => t('Time'),
      '%type' => 'Time',
    )), '', array(
      'attributes' => array(
        'class' => 'oh-add-more-link',
      ),
    ));
    $element['#prefix'] .= $link;
  }
  $element['#prefix'] .= '<div class="form-item office-hours-block" ' . $block_style . ' data-day="' . $day . '">';
  $element['#prefix'] .= '<label ' . $label_style . '>' . $label . '</label>';

  // Show a 'Clear this line' js-link to each element.
  // Use text 'Remove', which has lots of translations.
  $clear_link = l(t('Remove'), '', array(
    'attributes' => array(
      'class' => 'oh-clear-link',
    ),
  ));
  $element['#suffix'] .= '<div>' . $clear_link . '&nbsp;&nbsp;&nbsp;' . $same_link . '</div>';
  $element['#suffix'] .= '</div>';

  // class="form-item office-hours-block ".
  $element['day'] = array(
    '#type' => 'value',
    '#value' => $element['#day'],
  );
  $element['daydelta'] = array(
    '#type' => 'value',
    '#value' => $element['#daydelta'],
  );
  $element['starthours'] = array(
    '#type' => 'office_hours_select',
    '#default_value' => isset($element['#value']['starthours']) ? $element['#value']['starthours'] : NULL,
    '#field_settings' => $element['#field_settings'],
  );
  $element['endhours'] = array(
    '#type' => 'office_hours_select',
    '#prefix' => t("@from to @to", array(
      '@from' => '',
      '@to' => '',
    )),
    '#default_value' => isset($element['#value']['endhours']) ? $element['#value']['endhours'] : NULL,
    '#field_settings' => $element['#field_settings'],
  );
  $element['comment'] = array(
    '#type' => $has_comment ? 'textfield' : 'hidden',
    '#prefix' => $has_comment ? t('Comment') . ' ' : '',
    '#default_value' => isset($element['#value']['comment']) ? $element['#value']['comment'] : NULL,
    '#size' => 25,
    '#maxlength' => 255,
  );
  return $element;
}