Advertisement
benjamin_mcf

cludder.module

Dec 17th, 2011
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.92 KB | None | 0 0
  1. <?php
  2. /**
  3.  * @file
  4.  *
  5.  * Defines a custom field for constructing a custom poutine.
  6.  */
  7.  
  8. module_load_include('inc', 'cludder', 'cludder.selex');
  9.  
  10.  
  11. /**
  12.  * Implements hook_field_info().
  13.  *
  14.  * Here we tell Drupal about our custom fields. In this case
  15.  * we only have one. Its machine-readable name is 'poutine_maker_poutine'
  16.  * because the convention is 'modulename_fieldname'.
  17.  *
  18.  * We also define the machine names of the widget and the formatter.
  19.  */
  20. function cludder_field_info() {
  21.   return array(
  22.     'subject_field_type' => array(
  23.       'label' => t('Subject'),
  24.       'description' => t('Tagged by Subject'),
  25.       'default_widget' => 'subject_widget',
  26.       'default_formatter' => 'subject_formatter',
  27.     ),
  28.     'grade_field_type' => array(
  29.       'label' => t('Grade'),
  30.       'description' => t('Tagged by Grade'),
  31.       'default_widget' => 'grade_widget',
  32.       'default_formatter' => 'grade_formatter',
  33.     ),
  34.     'strand_field_type' => array(
  35.       'label' => t('Strand'),
  36.       'description' => t('Tagged by Strand'),
  37.       'default_widget' => 'strand_widget',
  38.       'default_formatter' => 'strand_formatter',
  39.     ),
  40.     'topic_field_type' => array(
  41.       'label' => t('Topic'),
  42.       'description' => t('Tagged by Topic'),
  43.       'default_widget' => 'topic_widget',
  44.       'default_formatter' => 'topic_formatter',
  45.     ),
  46.   );
  47. }
  48.  
  49.  
  50. /**
  51.  * Implements hook_field_widget_info().
  52.  *
  53.  * Here we tell Drupal about our custom widgets. In this
  54.  * case we only have one. As with poutine_maker_field_formatter_info(),
  55.  * we tell Drupal which fields our widget works with (in this case, just
  56.  * 'poutine_maker_poutine').
  57.  */
  58. function cludder_field_widget_info() {
  59.   return array(
  60.     'subject_widget' => array(
  61.       'label' => t('Default'),
  62.       'field types' => array('subject_field_type'),
  63.     ),
  64.     'grade_widget' => array(
  65.       'label' => t('Default'),
  66.       'field types' => array('grade_field_type'),
  67.     ),
  68.     'strand_widget' => array(
  69.       'label' => t('Default'),
  70.       'field types' => array('strand_field_type'),
  71.     ),
  72.     'topic_widget' => array(
  73.       'label' => t('Default'),
  74.       'field types' => array('topic_field_type'),
  75.     ),
  76.   );
  77. }
  78.  
  79.  
  80. /**
  81.  * Implements hook_field_widget_form().
  82.  *
  83.  * http://api.drupal.org/api/drupal/modules--field--field.api.php/function/hook_field_widget_form/7
  84.  *
  85.  * Here we define a form element that the user inputs data
  86.  * into. If we have a complex custom field, we can have many sub-elements
  87.  * organized into fieldsets.
  88.  */
  89. function cludder_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
  90.   // $item is where the current saved values are stored
  91.   $item =& $items[$delta];
  92.  
  93.   $subjids = array();  
  94.   if (!empty($form_state['values']['subject'])) {
  95.     $subjids = array_filter($form_state['values']['subject']);
  96.   }
  97.   $gradids = array();
  98.   if (!empty($form_state['values']['grade'])) {
  99.     $gradids = array_filter($form_state['values']['grade']);
  100.   }
  101.   $strandids = array();
  102.   if (!empty($form_state['values']['strand'])) {
  103.     $strandids = array_filter($form_state['values']['strand']);
  104.   }
  105.   $topicids = array();
  106.   if (!empty($form_state['values']['topics'])) {
  107.     $topicids = array_filter($form_state['values']['topics']);
  108.   }
  109.  
  110.   if ($instance['widget']['type'] == 'subject_widget') {
  111.  
  112.       $element['subjectblock'] = array(
  113.             '#type'     => 'fieldset',
  114.             '#title'     => 'Subjects',
  115.       );  
  116.       $element['subjectblock']['subject'] = array(
  117.             '#type'     => 'checkboxes',
  118.             '#options' => grab_subjects(),
  119.             '#prefix' => '<div style="background:#FF7F55;">',
  120.             '#suffix' => '</div>',
  121.             //'#default_value' => remember_subjects(),
  122.             '#ajax' => array(
  123.                 'callback' => 'make_topic_chex_callback',
  124.                 'progress' => array('type' => 'none'),
  125.             ),
  126.       );
  127.  
  128.   }
  129.  
  130.   if ($instance['widget']['type'] == 'grade_widget') {
  131.  
  132.       $element['gradeblock'] = array(
  133.             '#type'     => 'fieldset',
  134.             '#title'     => 'Grades',
  135.       );
  136.       $element['gradeblock']['grade'] = array(
  137.             '#type'     => 'checkboxes',
  138.             '#options' => grab_grades(),
  139.             '#prefix' => '<div style="background:#557FFF;">',
  140.             '#suffix' => '</div>',
  141.             '#ajax' => array(
  142.                 'callback' => 'make_topic_chex_callback',
  143.                 'progress' => array('type' => 'none'),
  144.             ),
  145.       );
  146.  
  147.   }
  148.  
  149.   if ($instance['widget']['type'] == 'strand_widget') {
  150.  
  151.       $element['blanx'] = array(
  152.             '#type'     => 'markup',
  153.             '#prefix' => '<div>',
  154.             '#suffix' => '</div>',
  155.       );  
  156.       if (!empty($form_state['values']['subject']) && !empty($form_state['values']['grade'])) {
  157.           $element['strandwrap'] = array(
  158.                 '#type'     => 'markup',
  159.                 '#prefix' => '<div id="pow">',
  160.                 '#suffix' => '</div>',
  161.           );  
  162.       }
  163.       if (empty($form_state['values']['subject']) && empty($form_state['values']['grade'])) {
  164.           $element['strandwrap'] = array(
  165.                 '#type'     => 'hidden',
  166.                 '#prefix' => '<div id="pow">',
  167.                 '#suffix' => '</div>',
  168.           );  
  169.       }
  170.       $element['strandwrap']['strandblock'] = array(
  171.             '#type'     => 'fieldset',
  172.             '#title'     => 'Strands',
  173.       );
  174.       $element['strandwrap']['strandblock']['strand'] = array(
  175.             '#type'     => 'checkboxes',
  176.             '#options' => grab_strands($subjids, $gradids),
  177.             '#prefix' => '<div style="background:#FFFF2A;">',
  178.             '#suffix' => '</div>',
  179.             '#ajax' => array(
  180.                 'callback' => 'make_topic_chex_callback',
  181.                 'progress' => array('type' => 'none'),
  182.             ),
  183.       );
  184.  
  185.   }
  186.  
  187.   if ($instance['widget']['type'] == 'topic_widget') {
  188.  
  189.       if (!empty($form_state['values']['subject']) && !empty($form_state['values']['grade'])) {
  190.           $element['topwrap'] = array(
  191.                 '#type'     => 'markup',
  192.                 '#prefix' => '<div id="gunge">',
  193.                 '#suffix' => '</div>',
  194.           );
  195.       }
  196.       if (empty($form_state['values']['subject']) && empty($form_state['values']['grade'])) {
  197.           $element['topwrap'] = array(
  198.                 '#type'     => 'hidden',
  199.                 '#prefix' => '<div id="gunge">',
  200.                 '#suffix' => '</div>',
  201.           );
  202.       }
  203.       $element['topwrap']['topblock'] = array(
  204.                 '#type'   => 'fieldset',
  205.                 '#title'  => 'Topics',
  206.       );
  207.       $element['topwrap']['topblock']['topics'] = array(
  208.             '#type'     => 'checkboxes',
  209.             '#options' => grab_topics($subjids, $gradids, $strandids),
  210.             '#prefix' => '<div style="background:#FFAA00;">',
  211.             '#suffix' => '</div>',
  212.       );
  213.  
  214.   }
  215.  
  216.   return $element;
  217.  
  218. }
  219.  
  220. function make_topic_chex_callback(&$form, $form_state) {
  221.     $parents_subject = $form_state['subject']['#parents'];
  222.     $parents_grade = $form_state['grade']['#parents'];
  223.     $commands = array();
  224.     $subz = array_filter($form_state['values']['subject']);
  225.     $grdz = array_filter($form_state['values']['grade']);
  226.     $isempty_subz = $subz ? 'TRUE' : 'FALSE';
  227.     $isempty_grdz = $grdz ? 'TRUE' : 'FALSE';
  228.     if ($isempty_subz == 'TRUE' && $isempty_grdz == 'TRUE' ) {
  229.         $commands[] = ajax_command_replace("#gunge", render($form['topwrap']));
  230.     }
  231.     else {
  232.         $commands[] = ajax_command_replace("#gunge", "<div id='gunge'>");
  233.     }
  234.     if ($isempty_subz == 'TRUE' && (in_array(15, $grdz) || in_array(16, $grdz)) ) {
  235.         $commands[] = ajax_command_replace("#pow", render($form['strandwrap']));
  236.     }
  237.     else {
  238.         $commands[] = ajax_command_replace("#pow", "<div id='pow'>");
  239.     }
  240.     return array('#type' => 'ajax', '#commands' => $commands);
  241. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement