Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * @file
- *
- * Defines a custom field for constructing a custom poutine.
- */
- module_load_include('inc', 'cludder', 'cludder.selex');
- /**
- * Implements hook_field_info().
- *
- * Here we tell Drupal about our custom fields. In this case
- * we only have one. Its machine-readable name is 'poutine_maker_poutine'
- * because the convention is 'modulename_fieldname'.
- *
- * We also define the machine names of the widget and the formatter.
- */
- function cludder_field_info() {
- return array(
- 'subject_field_type' => array(
- 'label' => t('Subject'),
- 'description' => t('Tagged by Subject'),
- 'default_widget' => 'subject_widget',
- 'default_formatter' => 'subject_formatter',
- ),
- 'grade_field_type' => array(
- 'label' => t('Grade'),
- 'description' => t('Tagged by Grade'),
- 'default_widget' => 'grade_widget',
- 'default_formatter' => 'grade_formatter',
- ),
- 'strand_field_type' => array(
- 'label' => t('Strand'),
- 'description' => t('Tagged by Strand'),
- 'default_widget' => 'strand_widget',
- 'default_formatter' => 'strand_formatter',
- ),
- 'topic_field_type' => array(
- 'label' => t('Topic'),
- 'description' => t('Tagged by Topic'),
- 'default_widget' => 'topic_widget',
- 'default_formatter' => 'topic_formatter',
- ),
- );
- }
- /**
- * Implements hook_field_widget_info().
- *
- * Here we tell Drupal about our custom widgets. In this
- * case we only have one. As with poutine_maker_field_formatter_info(),
- * we tell Drupal which fields our widget works with (in this case, just
- * 'poutine_maker_poutine').
- */
- function cludder_field_widget_info() {
- return array(
- 'subject_widget' => array(
- 'label' => t('Default'),
- 'field types' => array('subject_field_type'),
- ),
- 'grade_widget' => array(
- 'label' => t('Default'),
- 'field types' => array('grade_field_type'),
- ),
- 'strand_widget' => array(
- 'label' => t('Default'),
- 'field types' => array('strand_field_type'),
- ),
- 'topic_widget' => array(
- 'label' => t('Default'),
- 'field types' => array('topic_field_type'),
- ),
- );
- }
- /**
- * Implements hook_field_widget_form().
- *
- * http://api.drupal.org/api/drupal/modules--field--field.api.php/function/hook_field_widget_form/7
- *
- * Here we define a form element that the user inputs data
- * into. If we have a complex custom field, we can have many sub-elements
- * organized into fieldsets.
- */
- function cludder_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
- // $item is where the current saved values are stored
- $item =& $items[$delta];
- $subjids = array();
- if (!empty($form_state['values']['subject'])) {
- $subjids = array_filter($form_state['values']['subject']);
- }
- $gradids = array();
- if (!empty($form_state['values']['grade'])) {
- $gradids = array_filter($form_state['values']['grade']);
- }
- $strandids = array();
- if (!empty($form_state['values']['strand'])) {
- $strandids = array_filter($form_state['values']['strand']);
- }
- $topicids = array();
- if (!empty($form_state['values']['topics'])) {
- $topicids = array_filter($form_state['values']['topics']);
- }
- if ($instance['widget']['type'] == 'subject_widget') {
- $element['subjectblock'] = array(
- '#type' => 'fieldset',
- '#title' => 'Subjects',
- );
- $element['subjectblock']['subject'] = array(
- '#type' => 'checkboxes',
- '#options' => grab_subjects(),
- '#prefix' => '<div style="background:#FF7F55;">',
- '#suffix' => '</div>',
- //'#default_value' => remember_subjects(),
- '#ajax' => array(
- 'callback' => 'make_topic_chex_callback',
- 'progress' => array('type' => 'none'),
- ),
- );
- }
- if ($instance['widget']['type'] == 'grade_widget') {
- $element['gradeblock'] = array(
- '#type' => 'fieldset',
- '#title' => 'Grades',
- );
- $element['gradeblock']['grade'] = array(
- '#type' => 'checkboxes',
- '#options' => grab_grades(),
- '#prefix' => '<div style="background:#557FFF;">',
- '#suffix' => '</div>',
- '#ajax' => array(
- 'callback' => 'make_topic_chex_callback',
- 'progress' => array('type' => 'none'),
- ),
- );
- }
- if ($instance['widget']['type'] == 'strand_widget') {
- $element['blanx'] = array(
- '#type' => 'markup',
- '#prefix' => '<div>',
- '#suffix' => '</div>',
- );
- if (!empty($form_state['values']['subject']) && !empty($form_state['values']['grade'])) {
- $element['strandwrap'] = array(
- '#type' => 'markup',
- '#prefix' => '<div id="pow">',
- '#suffix' => '</div>',
- );
- }
- if (empty($form_state['values']['subject']) && empty($form_state['values']['grade'])) {
- $element['strandwrap'] = array(
- '#type' => 'hidden',
- '#prefix' => '<div id="pow">',
- '#suffix' => '</div>',
- );
- }
- $element['strandwrap']['strandblock'] = array(
- '#type' => 'fieldset',
- '#title' => 'Strands',
- );
- $element['strandwrap']['strandblock']['strand'] = array(
- '#type' => 'checkboxes',
- '#options' => grab_strands($subjids, $gradids),
- '#prefix' => '<div style="background:#FFFF2A;">',
- '#suffix' => '</div>',
- '#ajax' => array(
- 'callback' => 'make_topic_chex_callback',
- 'progress' => array('type' => 'none'),
- ),
- );
- }
- if ($instance['widget']['type'] == 'topic_widget') {
- if (!empty($form_state['values']['subject']) && !empty($form_state['values']['grade'])) {
- $element['topwrap'] = array(
- '#type' => 'markup',
- '#prefix' => '<div id="gunge">',
- '#suffix' => '</div>',
- );
- }
- if (empty($form_state['values']['subject']) && empty($form_state['values']['grade'])) {
- $element['topwrap'] = array(
- '#type' => 'hidden',
- '#prefix' => '<div id="gunge">',
- '#suffix' => '</div>',
- );
- }
- $element['topwrap']['topblock'] = array(
- '#type' => 'fieldset',
- '#title' => 'Topics',
- );
- $element['topwrap']['topblock']['topics'] = array(
- '#type' => 'checkboxes',
- '#options' => grab_topics($subjids, $gradids, $strandids),
- '#prefix' => '<div style="background:#FFAA00;">',
- '#suffix' => '</div>',
- );
- }
- return $element;
- }
- function make_topic_chex_callback(&$form, $form_state) {
- $parents_subject = $form_state['subject']['#parents'];
- $parents_grade = $form_state['grade']['#parents'];
- $commands = array();
- $subz = array_filter($form_state['values']['subject']);
- $grdz = array_filter($form_state['values']['grade']);
- $isempty_subz = $subz ? 'TRUE' : 'FALSE';
- $isempty_grdz = $grdz ? 'TRUE' : 'FALSE';
- if ($isempty_subz == 'TRUE' && $isempty_grdz == 'TRUE' ) {
- $commands[] = ajax_command_replace("#gunge", render($form['topwrap']));
- }
- else {
- $commands[] = ajax_command_replace("#gunge", "<div id='gunge'>");
- }
- if ($isempty_subz == 'TRUE' && (in_array(15, $grdz) || in_array(16, $grdz)) ) {
- $commands[] = ajax_command_replace("#pow", render($form['strandwrap']));
- }
- else {
- $commands[] = ajax_command_replace("#pow", "<div id='pow'>");
- }
- return array('#type' => 'ajax', '#commands' => $commands);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement