You are here

function taxonomy_image_display_overview_form_submit in Taxonomy Image 7

Same name and namespace in other branches
  1. 8 taxonomy_image.module \taxonomy_image_display_overview_form_submit()

Form submission handler for field_ui_display_overview_form().

Attaches the taxonomy_image field to vocabulary terms, if the image formatter is selected and the taxonomy_image field hasn't been instaciated for the vocabulary yet.

1 string reference to 'taxonomy_image_display_overview_form_submit'
taxonomy_image_form_field_ui_display_overview_form_alter in ./taxonomy_image.module
Implements hook_form_FORM_ID_alter() for field_ui_display_overview_form().

File

./taxonomy_image.module, line 239
Implements a field formatter that can display image on referenced taxonomy terms.

Code

function taxonomy_image_display_overview_form_submit(&$form, &$form_state) {

  // Iterate over the fields that are using the
  // taxonomy_image_term_reference_image formatter.
  foreach ($form['#fields'] as $field_name) {
    $field_info = field_info_field($field_name);
    if ($field_info['type'] == 'taxonomy_term_reference') {
      if ($form_state['values']['fields'][$field_name]['type'] == 'taxonomy_image_term_reference_image') {

        // If a field is available, don't do anything.
        if (count(_taxonomy_image_available_fields($field_info))) {
          continue;
        }

        // Lookup the associated vocabulary.
        $allowed_values = reset($field_info['settings']['allowed_values']);
        $vocabulary = $allowed_values['vocabulary'];

        // Ensure the taxonomy_image field exists.
        if (!field_info_field_by_id('taxonomy_image')) {
          try {
            field_create_field(array(
              'field_name' => 'taxonomy_image',
              'type' => 'image',
              'cardinality' => 1,
              'translatable' => TRUE,
              'entity_types' => array(
                'taxonomy_term',
              ),
            ));
          } catch (FieldException $e) {
            drupal_set_message(t('The default image field has been deleted.'), 'warning');
            return;
          }
        }

        // Attach an instance.
        field_create_instance(array(
          'field_name' => 'taxonomy_image',
          'entity_type' => 'taxonomy_term',
          'label' => t('Image'),
          'bundle' => $vocabulary,
          'description' => t('The image of this term.'),
          'required' => FALSE,
          'widget' => array(
            'type' => 'image_image',
          ),
        ));

        // Tell the user where the field settings can be changed.
        $field_settings_url = 'admin/structure/taxonomy/' . $vocabulary . '/fields/taxonomy_image';
        drupal_set_message(t('An image field has been attached to the vocabulary terms. You can adjust the !field_settings.', array(
          '!field_settings' => l('field settings', $field_settings_url),
        )));
      }
    }
  }
}