You are here

function mass_contact_taxonomy_admin_edit in Mass Contact 7

Creates a form element for Category add/edit page.

Parameters

array $recipients: The list of items for this category. For this plugin implementation, it is an array of taxonomy term IDs.

Return value

null|array The form snippet.

1 string reference to 'mass_contact_taxonomy_admin_edit'
mass_contact_taxonomy.inc in plugins/mass_contact_taxonomy.inc

File

plugins/mass_contact_taxonomy.inc, line 125

Code

function mass_contact_taxonomy_admin_edit(array $recipients) {

  // Get the list of all fields on the user entity.
  $user_fields = field_info_instances('user');
  if (empty($user_fields)) {
    return;
  }
  $taxonomy_fields = array();

  // Iterate through all the fields attached to the user entity.
  foreach ($user_fields['user'] as $user_field_instance) {

    // Get the field's information.
    $field = field_info_field($user_field_instance['field_name']);
    if (isset($field['module']) && $field['module'] == 'taxonomy') {

      // If the module providing this field is Taxonomy, add the field to the
      // field_name => vocabulary array of taxonomy fields.
      $taxonomy_fields[$field['field_name']] = $field['settings']['allowed_values'][0]['vocabulary'];
    }
  }
  $form_element = array();

  // Iterate through the array of fields.
  foreach ($taxonomy_fields as $field_name => $vocabulary_name) {
    $vocabulary = taxonomy_vocabulary_machine_name_load($vocabulary_name);
    if (!empty($vocabulary)) {
      $terms = taxonomy_get_tree($vocabulary->vid);
      if (!empty($terms)) {
        $options = array();
        foreach ($terms as $term) {
          $options[$term->tid] = check_plain($term->name);
        }
        $default_value = array();
        if (isset($recipients['mass_contact_taxonomy'])) {
          $default_value = $recipients['mass_contact_taxonomy'];
        }

        // Create a set of checkboxes, including each possible term.
        $form_element[$field_name] = array(
          '#type' => 'checkboxes',
          '#title' => t('Taxonomy vocabulary: %vocabulary', array(
            '%vocabulary' => $vocabulary->name,
          )),
          '#options' => $options,
          '#default_value' => $default_value,
        );
      }
    }
  }
  return $form_element;
}