You are here

collageformatter.module in Collage Formatter 8

Same filename and directory in other branches
  1. 7 collageformatter.module

Main file for Collage Formatter module.

File

collageformatter.module
View source
<?php

/**
 * @file
 * Main file for Collage Formatter module.
 */
use Drupal\image\Entity\ImageStyle;
use Drupal\Core\Form\FormStateInterface;

/**
 * Implements hook_theme()
 */
function collageformatter_theme() {
  return [
    'collageformatter_collage' => [
      'variables' => [
        'collage' => NULL,
        'collage_wrapper_class' => NULL,
        'collage_wrapper_style' => NULL,
        'collage_style' => NULL,
        'collage_bottom_style' => NULL,
      ],
    ],
    'collageformatter_collage_box' => [
      'variables' => [
        'box' => NULL,
        'box_style' => NULL,
      ],
    ],
    'collageformatter_collage_image' => [
      'variables' => [
        'image' => NULL,
        'image_wrapper_class' => NULL,
        'image_wrapper_style' => NULL,
        'image_style' => NULL,
      ],
    ],
  ];
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function collageformatter_form_entity_view_display_edit_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  if (count($form['fields']['field_image']['plugin']['settings_edit_form']) > 0 && $form_state
    ->getFormObject()
    ->getEntity()
    ->getRenderer('field_image')
    ->getPluginId() == 'collageformatter') {
    $form['fields']['field_image']['plugin']['settings_edit_form']['actions']['flush'] = [
      '#type' => 'submit',
      '#value' => t('Flush generated images'),
      '#submit' => [
        'collageformatter_flush_style_submit',
      ],
    ];
  }
}

/**
 * Flushes collageformatter style images.
 */
function collageformatter_flush_style_submit() {
  if (ImageStyle::load('collageformatter')) {
    $style = ImageStyle::load('collageformatter');
    $style
      ->flush();
    drupal_set_message(t('Style %style has been flushed.', [
      '%style' => 'collageformatter',
    ]));
  }
  else {
    drupal_set_message(t('Style %style is not found.', [
      '%style' => 'collageformatter',
    ]));
  }
}

/**
 * Implements hook_module_implements_alter().
 */
function collageformatter_module_implements_alter(&$implementations, $hook) {
  if ($hook == 'form_alter' && isset($implementations['collageformatter'])) {
    $group = $implementations['collageformatter'];
    unset($implementations['collageformatter']);
    $implementations['collageformatter'] = $group;
  }
}