You are here

function imageapi_optimize_pipeline_form in Image Optimize (or ImageAPI Optimize) 7.2

Form builder; Edit an image pipeline name and effects order.

Parameters

$form_state: An associative array containing the current state of the form.

$pipeline: An image pipeline array.

See also

image_pipeline_form_submit()

1 string reference to 'imageapi_optimize_pipeline_form'
imageapi_optimize_menu in ./imageapi_optimize.module
Implements hook_menu().

File

./imageapi_optimize.admin.inc, line 51

Code

function imageapi_optimize_pipeline_form($form, &$form_state, $pipeline) {
  $title = t('Edit %name pipeline', array(
    '%name' => $pipeline['label'],
  ));
  drupal_set_title($title, PASS_THROUGH);

  // Adjust this form for pipelines that must be overridden to edit.
  $editable = (bool) ($pipeline['storage'] & IMAGEAPI_OPTIMIZE_STORAGE_EDITABLE);
  if (!$editable && empty($form_state['input'])) {
    drupal_set_message(t('This image pipeline is currently being provided by a module. Click the "Override defaults" button to change its settings.'), 'warning');
  }
  $form_state['pipeline'] = $pipeline;
  $form['#tree'] = TRUE;
  $form['#attached']['css'][drupal_get_path('module', 'image') . '/image.admin.css'] = array();

  // Show the Image Pipeline label.
  $form['label'] = array(
    '#type' => 'textfield',
    '#title' => t('Imageapi optimize pipeline name'),
    '#default_value' => $pipeline['label'],
    '#disabled' => !$editable,
    '#required' => TRUE,
  );

  // Allow the name of the pipeline to be changed, unless this pipeline is
  // provided by a module's hook_default_image_pipelines().
  $form['name'] = array(
    '#type' => 'machine_name',
    '#size' => '64',
    '#default_value' => $pipeline['name'],
    '#disabled' => !$editable,
    '#description' => t('Use only lowercase alphanumeric characters, underscores (_), and hyphens (-).'),
    '#required' => TRUE,
    '#machine_name' => array(
      'exists' => 'imageapi_optimize_pipeline_load',
      'source' => array(
        'label',
      ),
      'replace_pattern' => '[^0-9a-z_\\-]',
      'error' => t('Please only use lowercase alphanumeric characters, underscores (_), and hyphens (-) for pipeline names.'),
    ),
  );

  // Build the list of existing image effects for this image pipeline.
  $form['processors'] = array(
    '#theme' => 'imageapi_optimize_pipeline_processors',
  );
  foreach ($pipeline['processors'] as $key => $processor) {
    $processor_handler = imageapi_optimize_processor_handler($processor);
    $form['processors'][$key]['#weight'] = isset($form_state['input']['processors']) ? $form_state['input']['processors'][$key]['weight'] : NULL;
    $form['processors'][$key]['label'] = array(
      '#markup' => $processor['label'],
    );
    $form['processors'][$key]['summary'] = array(
      '#markup' => $processor_handler
        ->getDescription(),
    );
    $form['processors'][$key]['weight'] = array(
      '#type' => 'weight',
      '#title' => t('Weight for @title', array(
        '@title' => $processor['label'],
      )),
      '#title_display' => 'invisible',
      '#default_value' => $processor['weight'],
      '#access' => $editable,
    );

    // Only attempt to display these fields for editable pipelines as the 'ieid'
    // key is not set for pipelines defined in code.
    if ($editable) {
      $form['processors'][$key]['configure'] = array(
        '#type' => 'link',
        '#title' => t('edit'),
        '#href' => 'admin/config/media/imageapi-optimize/edit/' . $pipeline['name'] . '/processors/' . $processor['ieid'],
        '#access' => $editable && is_a($processor_handler, 'ImageAPIOptimizeProcessorConfigurableInterface'),
      );
      $form['processors'][$key]['remove'] = array(
        '#type' => 'link',
        '#title' => t('delete'),
        '#href' => 'admin/config/media/imageapi-optimize/edit/' . $pipeline['name'] . '/processors/' . $processor['ieid'] . '/delete',
        '#access' => $editable,
      );
    }
  }

  // Build the new image effect addition form and add it to the effect list.
  $new_processor_options = array();
  foreach (imageapi_optimize_processor_definitions() as $processor => $definition) {
    $new_processor_options[$processor] = check_plain($definition['label']);
  }
  $form['processors']['new'] = array(
    '#tree' => FALSE,
    '#weight' => isset($form_state['input']['weight']) ? $form_state['input']['weight'] : NULL,
    '#access' => $editable,
  );
  $form['processors']['new']['new'] = array(
    '#type' => 'select',
    '#title' => t('Processor'),
    '#title_display' => 'invisible',
    '#options' => $new_processor_options,
    '#empty_option' => t('Select a new processor'),
  );
  $form['processors']['new']['weight'] = array(
    '#type' => 'weight',
    '#title' => t('Weight for new processor'),
    '#title_display' => 'invisible',
    '#default_value' => count($form['processors']) - 1,
  );
  $form['processors']['new']['add'] = array(
    '#type' => 'submit',
    '#value' => t('Add'),
    '#validate' => array(
      'imageapi_optimize_pipeline_form_add_validate',
    ),
    '#submit' => array(
      'imageapi_optimize_pipeline_form_submit',
      'imageapi_optimize_pipeline_form_add_submit',
    ),
  );

  // Show the Override or Submit button for this pipeline.
  $form['actions'] = array(
    '#type' => 'actions',
  );
  $form['actions']['override'] = array(
    '#type' => 'submit',
    '#value' => t('Override defaults'),
    '#validate' => array(),
    '#submit' => array(
      'imageapi_optimize_pipeline_form_override_submit',
    ),
    '#access' => !$editable,
  );
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Update pipeline'),
    '#access' => $editable,
  );
  return $form;
}