You are here

function iss_field_widget_form_alter in Image Style Selector 7.2

Implements hook_field_widget_form_alter().

File

./iss.module, line 94

Code

function iss_field_widget_form_alter(&$element, &$form_state, $context) {
  $field_type = $context['field']['type'];

  // We are handling only image fields right now.
  if ($field_type != 'image') {
    return;
  }

  // Fields "insert" enabled have choice of image style provided by insert module already.
  $widget_settings = $context['instance']['widget']['settings'];
  if (module_exists('insert') && isset($widget_settings['insert']) && $widget_settings['insert'] == 1) {
    return;
  }

  // Check if this field is iss enabled.
  if (empty($context['instance']['settings']['iss']['iss_enabled'])) {
    return;
  }
  if (!user_access('use iss')) {
    return;
  }
  $iss_config = $context['instance']['settings']['iss']['config'];
  $iss_config_source_styles = $iss_config['source_styles'];
  $iss_config_target_styles = $iss_config['target_styles'];
  $source_styles = array_intersect_key(iss_image_styles('source'), array_flip($iss_config_source_styles));
  $target_styles = array_intersect_key(iss_image_styles('target'), array_flip($iss_config_target_styles));
  foreach (element_children($element) as $key) {

    // If there is no file, don't render element.
    if (empty($element[$key]['#default_value']['fid'])) {
      continue;
    }
    if (count($source_styles) > 1) {
      $element[$key]['iss']['source'] = array(
        '#type' => 'select',
        '#title' => t('Source styles'),
        '#options' => $source_styles,
      );
    }
    $field_parents = $element[$key]['#field_parents'];
    $field_first_parent = array_shift($field_parents);
    if (!empty($field_parents)) {
      $name = $field_first_parent . '[' . implode('][', $field_parents) . '][' . $element[$key]['#field_name'] . '][' . $element[$key]['#language'] . '][' . $key . '][iss][sources]';
    }
    else {
      $name = $element[$key]['#field_name'] . '[' . $element[$key]['#language'] . '][' . $key . '][iss][source]';
    }
    foreach ($source_styles as $source_style => $source_style_label) {
      $element[$key]['iss']['iss_styles'][$source_style] = array(
        '#type' => 'select',
        '#title' => t('Target styles'),
        '#options' => $target_styles,
        '#states' => array(
          'visible' => array(
            "select[name=\"{$name}\"]" => array(
              'value' => $source_style,
            ),
          ),
        ),
      );
      if (!empty($context['items'][$key]['fid'])) {
        $style = db_query('SELECT iss.target_style FROM iss_styles iss WHERE iss.fid = :fid AND iss.source_style = :source', array(
          ':fid' => $context['items'][$key]['fid'],
          ':source' => $source_style,
        ))
          ->fetchField();
        if ($style) {
          $element[$key]['iss']['iss_styles'][$source_style]['#default_value'] = $style;
        }
      }
      foreach ($element[$key]['iss']['iss_styles'][$source_style]['#options'] as $machine_name => &$label) {
        $label = t($label);
      }

      // Change label of the source style in target styles.
      if (isset($element[$key]['iss']['iss_styles'][$source_style]['#options'][$source_style])) {
        $element[$key]['iss']['iss_styles'][$source_style]['#options'][$source_style] .= ' (' . t("default") . ')';
        if (empty($element[$key]['iss']['iss_styles'][$source_style]['#default_value'])) {
          $element[$key]['iss']['iss_styles'][$source_style]['#default_value'] = $source_style;
        }
      }
    }
  }
}