diff --git a/amars_conditional_fields.info.yml b/amars_conditional_fields.info.yml new file mode 100644 index 0000000..ab2ace2 --- /dev/null +++ b/amars_conditional_fields.info.yml @@ -0,0 +1,10 @@ +name: AMARS Conditional Fields +description: 'Custom Roblib module using API #states to set conditions on sighting report form fields for the AMARS project.' +package: Roblib + +type: module +core: 8.x + +#configure: amars_conditional_fields.list +dependencies: + - system (>=8.2.0) \ No newline at end of file diff --git a/amars_conditional_fields.module b/amars_conditional_fields.module new file mode 100644 index 0000000..16e5ca1 --- /dev/null +++ b/amars_conditional_fields.module @@ -0,0 +1,151 @@ +' . t('About') . ''; + $output .= '

' . t('AMARS Conditional Fields module.') . '

'; + return $output; + + default: + } +} + +/** + * Implements hook_theme(). + */ +function amars_conditional_fields_theme() { + return [ + 'amars_conditional_fields' => [ + 'render element' => 'children', + ], + ]; +} + +/** + * Implements hook_form_alter(). + * + * On fox_sighting_report node add and edit, set conditional fields using API #states based on values of + * other form fields. + */ +function amars_conditional_fields_form_node_fox_sighting_report_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) { + + $form['field_report_frequency_more']['widget'][0]['value']['#states'] = [ + 'visible' => [ + ':input[name="field_report_frequency"]' => ['value' => 'Other (please specify below)'] + ] + ]; + + $form['field_report_count_more']['widget'][0]['value']['#states'] = [ + 'visible' => [ + ':input[name="field_report_count"]' => ['value' => '4 or more'] + ] + ]; + + $form['field_report_age_2']['widget']['#states'] = [ + 'visible' => [ + [':input[name="field_report_count"]' => ['value' => (string) '2']], + [':input[name="field_report_count"]' => ['value' => (string) '3']], + [':input[name="field_report_count"]' => ['value' => (string) '4 or more']], + ] + ]; + + $form['field_report_age_3']['widget']['#states'] = [ + 'visible' => [ + [':input[name="field_report_count"]' => ['value' => (string) '3']], + [':input[name="field_report_count"]' => ['value' => (string) '4 or more']], + ] + ]; + + $form['field_report_age_more']['widget'][0]['value']['#states'] = [ + 'visible' => [ + ':input[name="field_report_count"]' => ['value' => '4 or more'] + ] + ]; + + $form['field_fox_report_colour_2']['widget']['#states'] = [ + 'visible' => [ + [':input[name="field_report_count"]' => ['value' => (string) '2']], + [':input[name="field_report_count"]' => ['value' => (string) '3']], + [':input[name="field_report_count"]' => ['value' => (string) '4 or more']], + ] + ]; + + $form['field_fox_report_colour_3']['widget']['#states'] = [ + 'visible' => [ + [':input[name="field_report_count"]' => ['value' => (string) '3']], + [':input[name="field_report_count"]' => ['value' => (string) '4 or more']], + ] + ]; + + $form['field_fox_report_colour_more']['widget'][0]['value']['#states'] = [ + 'visible' => [ + ':input[name="field_report_count"]' => ['value' => '4 or more'] + ] + ]; + + $form['field_report_weather_more']['widget'][0]['value']['#states'] = [ + 'visible' => [ + ':input[name="field_report_weather"]' => ['value' => 'Other (please specify below)'] + ] + ]; + + return $form; +} + +/** + * Implements hook_form_alter(). + * + * On coyote_sighting_report node add and edit, set conditional fields using API #states based on values of + * other form fields. + */ +function amars_conditional_fields_form_node_coyote_sighting_report_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) { + + $form['field_report_count_more']['widget'][0]['value']['#states'] = [ + 'visible' => [ + ':input[name="field_report_count"]' => ['value' => '4 or more'] + ] + ]; + + return $form; +} + +/** + * Implements hook_form_alter(). + * + * On wildlife_sighting_report node add and edit, set conditional fields using API #states based on values of + * other form fields. + */ +function amars_conditional_fields_form_node_wildlife_sighting_report_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) { + + $form['field_report_feeder_more']['widget'][0]['value']['#states'] = [ + 'visible' => [ + ':input[name="field_report_feeder[Other (please specify below)]"]' => ['checked' => TRUE] + ] + ]; + + $form['field_report_feeder_animals_more']['widget'][0]['value']['#states'] = [ + 'visible' => [ + ':input[name="field_report_feeder_animals[Other (please specify below)]"]' => ['checked' => TRUE] + ] + ]; + + $form['field_report_animal_acts_more']['widget'][0]['value']['#states'] = [ + 'visible' => [ + ':input[name="field_report_animal_acts[Other (please specify below)]"]' => ['checked' => TRUE] + ] + ]; + + return $form; +} \ No newline at end of file diff --git a/amars_conditional_fields.routing.yml b/amars_conditional_fields.routing.yml new file mode 100644 index 0000000..d70eb27 --- /dev/null +++ b/amars_conditional_fields.routing.yml @@ -0,0 +1,6 @@ +amars_conditional_fields: + path: /roblib/{count} + defaults: + _controller: Drupal\amars_conditional_fields\Controller\RoblibController::roblib + requirements: + _permission: access content \ No newline at end of file diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..3e4aafa --- /dev/null +++ b/composer.json @@ -0,0 +1,14 @@ +{ + "name": "roblib/amars_conditional_fields", + "type": "drupal-custom-module", + "description": "Custom Roblib module using API #states to set conditions on sighting report form fields for the AMARS project.", + "keywords": ["Drupal"], + "license": "GPL-2.0+", + "homepage": "", + "minimum-stability": "dev", + "support": { + "issues": "", + "source": "" + }, + "require": { } +} \ No newline at end of file diff --git a/src/Controller/RoblibController.php b/src/Controller/RoblibController.php new file mode 100644 index 0000000..70bce86 --- /dev/null +++ b/src/Controller/RoblibController.php @@ -0,0 +1,19 @@ + \ No newline at end of file