You are here

class ImageBgModifier in Modifiers Pack 8

Provides a Modifier to set the image background on an element.

Plugin annotation


@Modifier(
  id = "image_bg_modifier",
  label = @Translation("Image Background Modifier"),
  description = @Translation("Provides a Modifier to set the image background on an element."),
)

Hierarchy

  • class \Drupal\modifiers_bg_image\Plugin\modifiers\ImageBgModifier extends \Drupal\modifiers\ModifierPluginBase

Expanded class hierarchy of ImageBgModifier

1 file declares its use of ImageBgModifier
ImageBgModifierTest.php in modules/modifiers_bg_image/tests/src/Unit/ImageBgModifierTest.php
1 string reference to 'ImageBgModifier'
ImageBgModifier::modification in modules/modifiers_bg_image/src/Plugin/modifiers/ImageBgModifier.php

File

modules/modifiers_bg_image/src/Plugin/modifiers/ImageBgModifier.php, line 17

Namespace

Drupal\modifiers_bg_image\Plugin\modifiers
View source
class ImageBgModifier extends ModifierPluginBase {

  /**
   * {@inheritdoc}
   */
  public static function modification($selector, array $config) {
    if (!empty($config['image'])) {
      $media = parent::getMediaQuery($config);
      $args['image'] = $config['image'];
      if (!empty($config['image_style'])) {
        switch ($config['image_style']) {
          case 'stretch':
            $args['size'] = 'cover';
            break;
          case 'no-repeat':
          case 'repeat':
          case 'repeat-x':
          case 'repeat-y':
            $args['repeat'] = $config['image_style'];
            break;
        }
      }
      if (!empty($config['image_position'])) {
        $args['position'] = str_replace('-', ' ', $config['image_position']);
      }
      if (!empty($config['bgi_color_val'])) {
        $args['color'] = $config['bgi_color_val'];
      }
      $libraries = [
        'modifiers_bg_image/apply',
      ];
      $settings = [
        'namespace' => 'ImageBgModifier',
        'callback' => 'apply',
        'selector' => $selector,
        'media' => $media,
        'args' => $args,
      ];
      $attributes[$media][$selector]['class'][] = 'modifiers-has-background';
      return new Modification([], $libraries, $settings, $attributes);
    }
    return NULL;
  }

}

Members