array( 'label' => 'Explain Field' ), ); } /** * Implementation of hook_field_settings(). */ function explainfield_field_settings($op, $field) { switch ($op) { case 'form': $form = array(); $form['allowed_values'] = array( '#type' => 'textarea', '#title' => t('Allowed values list'), '#default_value' => isset($field['allowed_values']) ? $field['allowed_values'] : '', '#required' => FALSE, '#multiple' => FALSE, '#rows' => 5, '#description' => t('The possible values this field can contain. Enter one value per line, in the format key|label. The key is the value that will be stored in the database and it must match the field storage type, %type. The label is optional and the key will be used as the label if no label is specified.', array('%type' => $field['type'])), ); $form['trigger_value'] = array( '#type' => 'textfield', '#title' => t('Trigger key'), '#default_value' => isset($field['trigger_value']) ? $field['trigger_value'] : '', '#required' => FALSE, '#description' => t('Select the key on which the explain field will trigger.'), ); $form['explain_options'] = array( '#type' => 'fieldset', '#title' => t('Explain field options'), '#collapsible' => TRUE, '#collapsed' => FALSE, ); $form['explain_options']['explain_label'] = array( '#type' => 'textfield', '#title' => t('Label'), '#default_value' => isset($field['explain_label']) ? $field['explain_label'] : '', '#required' => FALSE, '#description' => t('Label that will appear over the explain field, and when viewing the node.'), ); $form['explain_options']['explain_text'] = array( '#type' => 'textarea', '#title' => 'Help text for the explain field', '#default_value' => isset($field['explain_text']) ? $field['explain_text'] : '', '#required' => FALSE, '#rows' => 3, '#description' => t('Instructions on what to write into the appearing explain field.'), ); $form['explain_options']['max_length'] = array( '#type' => 'textfield', '#title' => t('Maximum length'), '#default_value' => isset($field['max_length']) ? $field['max_length'] : '', '#required' => FALSE, '#description' => t('The maximum length of the field in characters. Leave blank for an unlimited size.'), ); $form['explain_options']['max_length_notify'] = array( '#type' => 'checkbox', '#title' => t('Include notification of maximum length'), '#default_value' => isset($field['max_length_notify']) ? $field['max_length_notify'] : '', '#required' => FALSE, '#description' => t('If a maximum length is specified, check here to include a notification on the edit form. If any explain field help text is specified above, this notification would be appended to it.'), ); $form['explain_options']['explain_rows'] = array( '#type' => 'textfield', '#title' => t('Textfield size'), '#default_value' => isset($field['explain_rows']) ? $field['explain_rows'] : '', '#required' => FALSE, '#description' => t('The height of the explain field. Leave blank for default 3.'), ); return $form; case 'save': return array( 'allowed_values', 'trigger_value', 'explain_text', 'explain_label', 'max_length', 'max_length_notify', 'explain_rows', ); case 'database columns': $columns = array( 'value' => array( 'type' => 'text', 'not null' => FALSE, 'sortable' => TRUE, ), 'explainfield' => array( 'type' => 'text', 'not null' => FALSE, 'sortable' => TRUE, ), ); return $columns; } } /** * Implementation of hook_content_is_empty(). */ function explainfield_content_is_empty($item, $field) { if (empty($item['value']) && (string)$item['value'] !== '0') { return TRUE; } return FALSE; } /** * Implementation of hook_field_formatter_info(). */ function explainfield_field_formatter_info() { $formatters = array( 'default' => array( 'label' => t('Default'), 'field types' => array('explainfield'), 'multiple values' => CONTENT_HANDLE_CORE, ), 'plain' => array( 'label' => t('Plain Text'), 'field types' => array('explainfield'), 'multiple values' => CONTENT_HANDLE_CORE, ), ); return $formatters; } /** * Implementation of hook_theme(). */ function explainfield_theme() { $themes = array( 'explainfield_formatter_default' => array( 'arguments' => array('element' => NULL), ), 'explainfield_formatter_plain' => array( 'arguments' => array('element' => NULL), ), 'explainfield_select' => array( 'arguments' => array('element' => NULL), ), 'explainfield_buttons' => array( 'arguments' => array('element' => NULL), ), ); return $themes; } /** * Theme functions for explainfield. */ function theme_explainfield_formatter_default($element) { $source = $element['#item']; $field = content_fields($element['#field_name'], $element['#type_name']); if ($allowed = _explainfield_allowed_values($element)) { $class = isset($field['']) ? '-inline' : ''; $output = $allowed; if ($source['explainfield'] != '') { if ($field['explain_label'] != '') { $output .= '
'; $output .= $field['explain_label'] . '
'; } $output .= '
'; $output .= $source['explainfield'] . '
'; } return $output; } else { return $element['#item']['safe']; } } function theme_explainfield_formatter_plain($element) { return ($allowed = _explainfield_allowed_values($element)) ? $allowed : strip_tags($element['#item']['safe']); } function theme_explainfield_select($element) { return $element['#children']; } function theme_explainfield_buttons($element) { $field = content_fields($element['#field_name'], $element['#type_name']); if (count(content_allowed_values($field)) == 1) { $output = $element['#children']; $output = str_replace('type="radio"', 'type="checkbox"', $output); return $output; } else { return $element['#children']; } } /** * Local function for allowed values. */ function _explainfield_allowed_values($element) { $field = content_fields($element['#field_name'], $element['#type_name']); if (($allowed_values = content_allowed_values($field)) && isset($allowed_values[$element['#item']['value']])) { return $allowed_values[$element['#item']['value']]; } } /** * Implementation of hook_elements(). */ function explainfield_elements() { $elements = array( 'explainfield_select' => array( '#input' => TRUE, '#columns' => array('value', 'explainfield'), '#delta' => 0, '#process' => array('explainfield_select_process'), ), 'explainfield_buttons' => array( '#input' => TRUE, '#columns' => array('value', 'explainfield'), '#delta' => 0, '#process' => array('explainfield_buttons_process'), ), ); return $elements; } /** * Implementation of hook_widget_info(). */ function explainfield_widget_info() { $info = array( 'explainfield_select' => array( 'label' => 'Explainfield in select list', 'field types' => array('explainfield'), 'multiple values' => CONTENT_HANDLE_CORE, 'callbacks' => array( 'default value' => CONTENT_CALLBACK_DEFAULT, ), ), 'explainfield_buttons' => array( 'label' => 'Explainfield in list of check boxes/radio buttons', 'field types' => array('explainfield'), 'multiple values' => CONTENT_HANDLE_CORE, 'callbacks' => array( 'default value' => CONTENT_CALLBACK_DEFAULT, ), ), ); return $info; } /** * Implementation of hook_widget(). */ function explainfield_widget(&$form, &$form_state, $field, $items, $delta = 0) { switch ($field['widget']['type']) { case 'explainfield_select': $element = array( '#type' => 'explainfield_select', '#default_value' => isset($items[$delta]) ? $items[$delta] : array(), ); break; case 'explainfield_buttons': $element = array( '#type' => 'explainfield_buttons', '#default_value' => isset($items[$delta]) ? $items[$delta] : array(), ); break; } return $element; } function explainfield_select_process($element, $edit, $form_state, $form) { $defaults = $element['#value']; $field = content_fields($element['#field_name'], $element['#type_name']); $max_length = 0; if ($field['max_length'] && is_numeric($field['max_length'])) { $max_length = $field['max_length']; } $explain_rows = 3; if ($field['explain_rows'] && is_numeric($field['explain_rows'])) { $explain_rows = $field['explain_rows']; } static $pointer = 0; $pointer++; $field_key = $element['#columns'][0]; $prefix = "\n" . '
' . "\n"; $prefix .= ''; $prefix .= ''; $prefix .= ''; $prefix .= '' . "\n"; $options = content_allowed_values($field); $element[$field_key] = array( '#type' => 'select', '#default_value' => isset($defaults[$field_key]) ? $defaults[$field_key] : '', '#title' => $element['#title'], '#required' => $element['#required'], '#description' => $element['#description'], '#field_name' => $element['#field_name'], '#type_name' => $element['#type_name'], '#delta' => $element['#delta'], '#columns' => $element['#columns'], '#prefix' => $prefix, '#options' => $options, '#element_validate' => array('explainfield_select_validate'), ); $field_key = $element['#columns'][1]; $element[$field_key] = array( '#title' => $field['explain_label'], '#type' => $explain_rows == 1 ? 'textfield' : 'textarea', '#default_value' => isset($defaults[$field_key]) ? $defaults[$field_key] : '', '#rows' => $explain_rows, '#prefix' => '
', '#suffix' => '
', ); if ($field['explain_text']) { $element[$field_key]['#description'] = $field['explain_text']; if ($max_length > 0 && $field['max_length_notify']) { $element[$field_key]['#description'] .= t(' You can enter a maximum of %max characters for this field.', array('%max' => $max_length)); } } else if ($max_length > 0 && $field['max_length_notify']) { $element[$field_key]['#description'] = t('You can enter a maximum of %max characters for this field.', array('%max' => $max_length)); } return $element; } function explainfield_buttons_process($element, $edit, $form_state, $form) { $defaults = $element['#value']; $field = content_fields($element['#field_name'], $element['#type_name']); $max_length = 0; if ($field['max_length'] && is_numeric($field['max_length'])) { $max_length = $field['max_length']; } $explain_rows = 3; if ($field['explain_rows'] && is_numeric($field['explain_rows'])) { $explain_rows = $field['explain_rows']; } static $pointer = 0; $pointer++; $field_key = $element['#columns'][0]; $prefix = "\n" . '
' . "\n"; $prefix .= ''; $prefix .= ''; $prefix .= ''; $prefix .= '' . "\n"; $options = content_allowed_values($field); $element[$field_key] = array( '#type' => 'radios', '#default_value' => isset($defaults[$field_key]) ? $defaults[$field_key] : '', '#title' => $element['#title'], '#required' => $element['#required'], '#description' => $element['#description'], '#field_name' => $element['#field_name'], '#type_name' => $element['#type_name'], '#delta' => $element['#delta'], '#prefix' => $prefix, '#options' => $options, '#columns' => $element['#columns'], '#element_validate' => array('explainfield_buttons_validate'), ); $field_key = $element['#columns'][1]; $element[$field_key] = array( '#title' => $field['explain_label'], '#type' => $explain_rows == 1 ? 'textfield' : 'textarea', '#default_value' => isset($defaults[$field_key]) ? $defaults[$field_key] : '', '#rows' => $explain_rows, '#prefix' => '
', '#suffix' => '
' . "\n", ); if ($field['explain_text']) { $element[$field_key]['#description'] = $field['explain_text']; if ($max_length > 0 && $field['max_length_notify']) { $element[$field_key]['#description'] .= t(' You can enter a maximum of %max characters for this field.', array('%max' => $max_length)); } } else if ($max_length > 0 && $field['max_length_notify']) { $element[$field_key]['#description'] = t('You can enter a maximum of %max characters for this field.', array('%max' => $max_length)); } return $element; } function explainfield_select_validate($element, &$form_state) { $field_name = $element['#field_name']; $multiples_count = count($form_state['values'][$field_name]); $field = content_fields($element['#field_name'], $element['#type_name']); for ($i = 0; $i < $multiples_count; $i++) { // If the select/button/check value is not equal to the trigger value, there is // no reason to save any lingering value for the explain field itself. if ($form_state['values'][$field_name][$i]['value'] != $field['trigger_value']) { $form_state['values'][$field_name][$i]['explainfield'] = ''; } else { // Now make sure it *is* filled out and correctly so if it's supposed to be. if (trim($form_state['values'][$field_name][$i]['explainfield']) == '') { $error_field = $field_name . '][' . $i . '][explainfield'; form_set_error($error_field, t('Please fill the optional form in %label if you select this option.', array('%label' => t($field['widget']['label'])))); } else { if ($field['max_length'] > 0) { $error_field = $field['field_name'] .']['. $i .'][explainfield'; $data = $form_state['values'][$field_name][$i]; if (strlen($data['explainfield']) > $field['max_length']) { form_set_error($error_field, t('There is a limit of %max characters on %label; your current entry contains %used characters.', array('%max' => $field['max_length'], '%label' => $field['widget']['label'], '%used' => strlen($data['explainfield'])))); } } } } } } function explainfield_buttons_validate($element, &$form_state) { $field_name = $element['#field_name']; $multiples_count = count($form_state['values'][$field_name]); $field = content_fields($element['#field_name'], $element['#type_name']); for ($i = 0; $i < $multiples_count; $i++) { // If the select/button/check value is not equal to the trigger value, there is // no reason to save any lingering value for the explain field itself. if ($form_state['values'][$field_name][$i]['value'] != $field['trigger_value']) { $form_state['values'][$field_name][$i]['explainfield'] = ''; } else { // Now make sure it *is* filled out and correctly so if it's supposed to be. if (trim($form_state['values'][$field_name][$i]['explainfield']) == '') { $error_field = $field_name . '][' . $i . '][explainfield'; form_set_error($error_field, t('Please fill the optional form in %label if you select this option.', array('%label' => t($field['widget']['label'])))); } else { if ($field['max_length'] > 0) { $error_field = $field['field_name'] .']['. $i .'][explainfield'; $data = $form_state['values'][$field_name][$i]; if (strlen($data['explainfield']) > $field['max_length']) { form_set_error($error_field, t('There is a limit of %max characters on %label; your current entry contains %used characters.', array('%max' => $field['max_length'], '%label' => $field['widget']['label'], '%used' => strlen($data['explainfield'])))); } } } } } }