You are here

inline_responsive_image.module in Inline responsive images 8

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

File

inline_responsive_image.module
View source
<?php

use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\editor\Entity\Editor;

/**
 * Implements hook_theme().
 */
function inline_responsive_image_theme() {
  return array(
    'inline_responsive_image' => array(
      'variables' => array(
        'image' => '',
        'attributes' => array(),
        'caption' => '',
      ),
    ),
  );
}
function inline_responsive_image_form_editor_image_dialog_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
  $image_element = $form_state
    ->get('image_element') ?: [];
  $responsive_image_styles = entity_load_multiple('responsive_image_style');
  $image_styles = entity_load_multiple('image_style');
  $options = [];

  // Responsive image styles
  if (count($responsive_image_styles) > 0) {
    $responsive_label = new TranslatableMarkup('Responsive image styles');
    $responsive_label_string = (string) $responsive_label;
    $options[$responsive_label_string] = [];
    foreach ($responsive_image_styles as $key => $style) {
      $options[$responsive_label_string]['responsive:' . $key] = $style
        ->label();
    }
  }

  // Normal image styles
  if (count($image_styles) > 0) {
    $image_style_label = new TranslatableMarkup('Image styles');
    $image_style_label_string = (string) $image_style_label;
    $options[$image_style_label_string] = [];
    foreach ($image_styles as $key => $style) {
      $options[$image_style_label_string]['imagestyle:' . $key] = $style
        ->label();
    }
  }
  $form['style'] = array(
    '#type' => 'select',
    '#title' => t('Style'),
    '#options' => $options,
    '#empty_value' => '',
    '#default_value' => !empty($image_element['data-style']) ? $image_element['data-style'] : '',
    '#parents' => array(
      'attributes',
      'data-style',
    ),
  );
}

/**
 * Implements hook_ckeditor_css_alter().
 */
function inline_responsive_image_ckeditor_css_alter(array &$css, Editor $editor) {
  if (!$editor
    ->hasAssociatedFilterFormat()) {
    return;
  }
  if ($editor
    ->getFilterFormat()
    ->filters('inline_responsive_image')->status) {
    $css[] = drupal_get_path('module', 'inline_responsive_image') . '/css/plugins/drupalimagestyle/ckeditor.drupalimagestyle.css';
  }
}