imageapi_optimize.module 2.61 KB
Newer Older
Steven Jones's avatar
Steven Jones committed
1
2
3
4
5
6
7
8
9
10
11
12
<?php

use Drupal\imageapi_optimize\Entity\ImageAPIOptimizePipeline;

/**
 * Gets an array of image pipelines suitable for using as select list options.
 *
 * @param $include_empty
 *   If TRUE a '- None -' option will be inserted in the options array.
 * @return
 *   Array of image pipelines both key and value are set to pipeline name.
 */
13
function imageapi_optimize_pipeline_options($include_empty = TRUE, $include_site_default = TRUE) {
Steven Jones's avatar
Steven Jones committed
14
15
16
17
18
  $pipelines = ImageAPIOptimizePipeline::loadMultiple();
  $options = array();
  if ($include_empty && !empty($pipelines)) {
    $options[''] = t('- None -');
  }
19
  if ($include_site_default && !empty($pipelines)) {
20
21
22
23
24
    if ($default_pipeline_name = \Drupal::config('imageapi_optimize.settings')->get('default_pipeline')) {
      if ($default_pipeline = ImageAPIOptimizePipeline::load($default_pipeline_name)) {
        $options['__default__'] = t('Sitewide default pipeline: @name', array('@name' => $default_pipeline->label()));
      }
    }
25
  }
Steven Jones's avatar
Steven Jones committed
26
27
28
29
30
31
32
33
  foreach ($pipelines as $name => $pipeline) {
    $options[$name] = $pipeline->label();
  }

  if (empty($options)) {
    $options[''] = t('No defined pipelines');
  }
  return $options;
34
35
36
37
38
39
40
41
42
}

/**
 * Implements hook_entity_type_alter().
 */
function imageapi_optimize_entity_type_alter(array &$entity_types) {
  /** @var $entity_types \Drupal\Core\Entity\EntityTypeInterface[] */
  if (isset($entity_types['image_style'])) {
    $image_style = $entity_types['image_style'];
43
44
    $image_style->setClass('Drupal\imageapi_optimize\Entity\ImageStyleWithPipeline');
    $image_style->setHandlerClass('list_builder', 'Drupal\imageapi_optimize\ImageStyleWithPipelineListBuilder');
45
46
47
48
49
50
    $config_export = $image_style->get('config_export');
    $config_export[] = 'pipeline';
    $image_style->set('config_export', $config_export);
  }
}

51
52
53
54
55
56
57
58
59
/**
 * Implements hook_config_schema_info_alter().
 */
function imageapi_optimize_config_schema_info_alter(&$definitions) {
  if (isset($definitions['image.style.*'])) {
    $definitions['image.style.*']['mapping']['pipeline']['type'] = 'string';
  }
}

60
61
62
63
64
65
66
/**
 * Implements hook_form_FORM_ID_alter().
 */
function imageapi_optimize_form_image_style_edit_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
  $entity = $form_state->getFormObject()->getEntity();
  $form['pipeline'] = [
    '#type' => 'select',
67
    '#title' => t('Image Optimize Pipeline'),
68
    '#options' => imageapi_optimize_pipeline_options(),
69
70
71
    '#default_value' => $entity->getPipeline(),
    '#description' => t('Optionally select an Image Optimization pipeline which will be applied after all effects in this image style.'),
    '#weight' => 10,
72
73
  ];
}