You are here

function manualcrop_auto_reuse_form in Manual Crop 7

Form Builder; Configuration settings for the auto reuse effect.

Parameters

$data: The current configuration for this effect.

Return value

The form structure array.

1 string reference to 'manualcrop_auto_reuse_form'
manualcrop_image_effect_info in ./manualcrop.module
Implements hook_image_effect_info().

File

./manualcrop.admin.inc, line 282
Admin functionality for the Manual Crop module.

Code

function manualcrop_auto_reuse_form($data) {

  // Load the crop image styles and exclude the style that's being edited.
  $styles = manualcrop_styles_with_crop(FALSE, 5, TRUE);
  if (empty($styles)) {
    drupal_set_message(t('No Manual Crop enabled image style could be found. To reuse a crop selection, you need to create at least one image style that uses Manual Crop.'), 'warning');
    drupal_goto('admin/config/media/image-styles/edit/' . arg(5));
  }

  // Table with all crop image styles.
  $form['style_priority'] = array(
    '#tree' => TRUE,
    '#header' => array(
      t('Image style'),
      t('Weight'),
    ),
    '#rows' => array(),
    '#theme' => 'table',
    '#attributes' => array(
      'id' => 'manualcrop-style-priority',
    ),
  );
  if (empty($data['style_priority'])) {

    // Make sure the style_priority key exists.
    $data['style_priority'] = array();
  }
  else {

    // Put the styles in their correct order.
    $styles = array_merge(array_fill_keys($data['style_priority'], 0), $styles);
    $styles = array_filter($styles);
  }

  // Add each crop image style as row with a weight (priority) element.
  $maxweight = count($data['style_priority']);
  foreach ($styles as $style_name => $label) {
    $weight = array_search($style_name, $data['style_priority']);
    $weight = array(
      '#type' => 'weight',
      '#default_value' => $weight === FALSE ? $maxweight : $weight,
      '#attributes' => array(
        'class' => array(
          'manualcrop-style-priority-weight',
        ),
      ),
    );
    $form['style_priority'][$style_name] =& $weight;
    $form['style_priority']['#rows'][] = array(
      'data' => array(
        array(
          'data' => check_plain($label),
        ),
        array(
          'data' => &$weight,
        ),
      ),
      'class' => array(
        'draggable',
      ),
    );
    unset($weight);
  }

  // Add a tabledrag for a cleaner interface.
  drupal_add_tabledrag('manualcrop-style-priority', 'order', 'sibling', 'manualcrop-style-priority-weight');

  // Get a list of allowed fallback styles.
  $styles = array_diff_key(image_styles(), manualcrop_styles_with_crop(TRUE));
  $styles = array_map('_manualcrop_image_style_label', $styles);
  $styles = array(
    '' => t('None'),
  ) + $styles;

  // Remove the current style.
  if (isset($styles[arg(5)])) {
    unset($styles[arg(5)]);
  }
  $form['apply_all_effects'] = array(
    '#type' => 'checkbox',
    '#title' => t('Apply all effects'),
    '#description' => t('Apply all effects of the reused image style.'),
    '#default_value' => !empty($data['apply_all_effects']),
  );
  $form['fallback_style'] = array(
    '#type' => 'select',
    '#title' => t('Fallback image style'),
    '#description' => t('Image style to use if no cropping selection is found for an image.'),
    '#options' => $styles,
    '#default_value' => isset($data['fallback_style']) ? $data['fallback_style'] : '',
  );
  return $form;
}