You are here

function taxonomy_image_display_overview_form_submit in Taxonomy Image 8

Same name and namespace in other branches
  1. 7 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_entity_view_display_edit_form_alter in ./taxonomy_image.module
Implements hook_form_FORM_BASE_ID_alter().

File

./taxonomy_image.module, line 22
Implements field formatter that displays image on referenced taxonomy terms.

Code

function taxonomy_image_display_overview_form_submit(array $form, FormStateInterface $form_state) {

  // Content type machine name.
  $type = $form['#bundle'];

  // Iterate over the fields that are using the
  // taxonomy_image_term_reference_image formatter.
  foreach ($form['#fields'] as $field_name) {
    $entity_type = 'node';
    $field_info = FieldStorageConfig::loadByName($entity_type, $field_name);
    if ($field_info) {
      if ($field_info
        ->getType() == 'entity_reference') {
        if ($form_state
          ->getValue('fields')[$field_name]['type'] == 'taxonomy_image_term_reference_image') {

          // If a field is available, don't do anything.
          // Lookup the associated vocabulary.
          $field_data = \Drupal::config('field.field.node.' . $type . "." . $field_name)
            ->get();
          $bundles = $field_data['settings']['handler_settings']['target_bundles'];
          $bundle = reset($bundles);

          // Ensure the taxonomy_image field exists.
          $entity_type_id = 'taxonomy_term';
          $image_field_name = 'taxonomy_image';
          if (!FieldConfig::loadByName($entity_type_id, $bundle, $image_field_name)) {
            if (!FieldStorageConfig::loadByName($entity_type_id, $image_field_name)) {
              try {
                FieldStorageConfig::create(array(
                  'field_name' => $image_field_name,
                  'entity_type' => 'taxonomy_term',
                  'type' => 'image',
                ))
                  ->save();
              } catch (FieldException $e) {
                drupal_set_message(t('The default image field has been deleted.'), 'warning');
              }
            }
            try {
              FieldConfig::create(array(
                'field_name' => $image_field_name,
                'entity_type' => 'taxonomy_term',
                'bundle' => $bundle,
                'label' => t('Image'),
                'description' => t('The image of this term.'),
                'required' => FALSE,
              ))
                ->save();

              // Tell the user where the field settings can be changed.
              $field_settings_url = '/admin/structure/taxonomy/manage/' . $bundle . '/overview/fields/taxonomy_term.' . $bundle . '.' . $image_field_name;
              $link = Link::fromTextAndUrl('field settings', Url::fromUri('internal:' . $field_settings_url))
                ->toString();
              drupal_set_message(t('An image field has been attached to the vocabulary terms. You can adjust the @field_settings.', array(
                '@field_settings' => $link,
              )));
            } catch (FieldException $e) {
              drupal_set_message(t('The default image field has been deleted.'), 'warning');
              return;
            }
          }
        }
      }
    }
  }
}