diff --git a/web/modules/custom/sfgov_departments/sfgov_departments.module b/web/modules/custom/sfgov_departments/sfgov_departments.module index e2bc96a68..59d950522 100644 --- a/web/modules/custom/sfgov_departments/sfgov_departments.module +++ b/web/modules/custom/sfgov_departments/sfgov_departments.module @@ -68,10 +68,12 @@ function _sfgov_departments_validate(&$form, FormStateInterface $form_state) { // TODO: remove this check after existing public bodies have been migrated to department/agency content type // $form['field_public_body_meetings']['widget']['#cardinality'] = 2; // this property assignment effectively prevents adding more things to know past 2, but there is no feedback regarding the restriction $thingCount = 0; - foreach ($meetingThingToKnow as $meetingThing) { - if (is_array($meetingThing)) { - if (!empty($meetingThing['subform'])) { - $thingCount++; + if (!empty($meetingThingToKnow)) { + foreach ($meetingThingToKnow as $meetingThing) { + if (is_array($meetingThing)) { + if (!empty($meetingThing['subform'])) { + $thingCount++; + } } } } @@ -302,22 +304,26 @@ function _sfgov_departments_dept_req_public_records_form_validate($form, FormSta */ function _sfgov_departments_agency_sections_alter_submit(&$form, FormStateInterface $form_state) { $values = $form_state->getValues(); - if (array_key_exists(0, $values['field_agency_sections'])) { - $label = $values['field_agency_sections'][0]['subform']['field_section_title_list']; - - // If the section title is set to "None" - if (empty($label) || $label[0]['value'] == '_none') { - - foreach ($values['field_agency_sections'][0]['subform']['field_agencies'] as $id => $item) { - // Remove each agency from the agency section. - if (is_numeric($id)) { - unset($values['field_agency_sections'][0]['subform']['field_agencies'][$id]); + if (!empty($values)) { + if (!empty($values['field_agency_sections'])) { + if (array_key_exists(0, $values['field_agency_sections'])) { + $label = $values['field_agency_sections'][0]['subform']['field_section_title_list']; + + // If the section title is set to "None" + if (empty($label) || $label[0]['value'] == '_none') { + + foreach ($values['field_agency_sections'][0]['subform']['field_agencies'] as $id => $item) { + // Remove each agency from the agency section. + if (is_numeric($id)) { + unset($values['field_agency_sections'][0]['subform']['field_agencies'][$id]); + } + } } + + // Reset the form values with the removed agencies. + $form_state->setValues($values); } } - - // Reset the form values with the removed agencies. - $form_state->setValues($values); } }