You are here

function birthdays_field_widget_form in Birthdays 7

Implements hook_field_widget_form().

File

./birthdays.module, line 597
The Birthdays module allows users to add their birthday to their profile. It lists birthdays on a seperate page and in different blocks. Users can receive an email on their birthday automatically, and the administrator can receive daily reminders of…

Code

function birthdays_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
  $element += array(
    '#tree' => TRUE,
    '#type' => 'birthdays_date',
    '#langcode' => $langcode,
    '#size' => 10,
    '#year' => $instance['settings']['hide_year'],
  );
  switch ($instance['widget']['type']) {
    case 'birthdays_date':

      // Provide the default date format settings.
      if (isset($instance['display']['default'])) {
        $default_display = $instance['display']['default'];
      }
      else {
        $default_display = reset($instance['display']);
      }
      $element['#display'] = $default_display['settings'];

      // Default value.
      if (!empty($items[$delta])) {
        $element['#default_value'] = $items[$delta];
      }
      break;
    case 'birthdays_textfield':

      // Setup the container.
      $element['#type'] = 'fieldset';
      $element['#dateformat'] = $instance['widget']['settings']['dateformat'];
      $element['#dateformat_noyear'] = str_ireplace(array(
        'y-',
        '-y',
        'y/',
        '/y',
        'y',
      ), '', $instance['widget']['settings']['dateformat']);

      // Add a child textfield.
      $element['value'] = array(
        '#type' => 'textfield',
        '#size' => 15,
        '#required' => $element['#required'],
      );

      // Set the default value.
      if (!empty($items[$delta]['day']) && !empty($items[$delta]['month'])) {
        $element['value']['#default_value'] = BirthdaysBirthday::fromArray($items[$delta])
          ->toString($element['#dateformat'], $element['#dateformat_noyear']);
      }

      // Set the placeholder attribute.
      switch ($element['#year']) {
        case BIRTHDAYS_HIDE_YEAR_NO:
          $element['value']['#attributes']['placeholder'] = $element['#dateformat'];
          break;
        case BIRTHDAYS_HIDE_YEAR_YES:
          $element['value']['#attributes']['placeholder'] = $element['#dateformat_noyear'];
          break;
        case BIRTHDAYS_HIDE_YEAR_USER:
          $element['value']['#attributes']['placeholder'] = t('@dateformat or @dateformat_noyear', array(
            '@dateformat' => $element['#dateformat'],
            '@dateformat_noyear' => $element['#dateformat_noyear'],
          ));
          break;
      }

      // Add validation callbacks.
      $element += array(
        '#element_validate' => array(
          '_birthdays_field_widget_textfield_validate',
          'birthdays_validate_date',
          'birthdays_validate_date_complete',
        ),
      );

      // No need to use a fieldset when there is no trigger checkbox.
      if (empty($instance['settings']['triggers']['user'])) {
        $element['#type'] = 'container';
        $element['value']['#title'] = $element['#title'];
      }

      // Datepicker poup.
      if ($instance['widget']['settings']['datepicker']) {
        drupal_add_library('system', 'ui.datepicker');
        if ($instance['settings']['hide_year'] == BIRTHDAYS_HIDE_YEAR_YES) {
          $element['value']['#attributes']['class'] = array(
            'birthdays-datepicker-noyear',
          );
        }
        else {
          $element['value']['#attributes']['class'] = array(
            'birthdays-datepicker',
          );
        }
        $element['value']['#attached']['js'][] = drupal_get_path('module', 'birthdays') . '/birthdays.js';
        $element['value']['#attached']['js'][] = array(
          'data' => array(
            'birthdays' => array(
              'firstDay' => variable_get('date_firstday', 0),
              'dateformat' => str_replace('Y', 'yy', $element['#dateformat']),
              'dateformat_noyear' => $element['#dateformat_noyear'],
            ),
          ),
          'type' => 'setting',
        );
      }
      break;
  }

  // The trigger checkbox.
  if (!empty($instance['settings']['triggers']['user'])) {
    $element['triggers'] = array(
      '#type' => 'checkbox',
      '#title' => check_plain($instance['settings']['triggers']['title']),
      '#description' => check_plain($instance['settings']['triggers']['description']),
      '#default_value' => isset($items[$delta]) ? $items[$delta]['triggers'] : TRUE,
      '#weight' => 3,
    );
  }
  else {
    $element['triggers'] = array(
      '#type' => 'value',
      '#value' => TRUE,
    );
  }
  return $element;
}