You are here

bg_img_field.module in Background Image Field 8

Provides a way to add background images dynamically.

File

bg_img_field.module
View source
<?php

/**
 * @file
 * Provides a way to add background images dynamically.
 */
use Drupal\Core\Url;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\RouteMatchInterface;

/**
 * Implements hook_help().
 */
function bg_img_field_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {
    case 'help.page.bg_img_field':
      $links = [
        ':field' => Url::fromRoute('help.page', [
          'name' => 'field',
        ])
          ->toString(),
        ':field_ui' => \Drupal::moduleHandler()
          ->moduleExists('field_ui') ? Url::fromRoute('help.page', [
          'name' => 'field_ui',
        ])
          ->toString() : '#',
      ];
      $output = '<h3>' . t('About') . '</h3>';
      $output .= '<p>';
      $output .= t('Background Image Field module allows you to create a field on an entity type. It requires responsive images mapping in order to offer the best image quality for the device it is rendering on. The field will define the CSS selector to attach the background image too and then allow you basic CSS options repeat, size, and position so you can define a  style per image in your content.');
      $output .= '</p>';
      $output .= '<p>';
      $output .= t('Administrators can add background image field on the <a href=":field_ui">Field UI</a> if module is enabled. For more information on fields see the <a href=":field">Field module help page</a>.', $links);
      $output .= '</p>';
      $output .= '<p>';
      $output .= t("Project documentation can be found on the project page, <a href='https://www.drupal.org/project/bg_img_field' target='_blank'>Background Image Field</a>.");
      $output .= '</p>';
      return $output;
  }
}

/**
 * Implements hook_form_alter().
 */
function bg_img_field_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  if ($form_id === 'field_config_edit_form') {
    $field_widget_storage = $form_state
      ->getStorage();
    if (isset($field_widget_storage['default_value_widget'])) {
      $pluginId = $field_widget_storage['default_value_widget']
        ->getPluginId();
      if ($pluginId === 'bg_img_field_widget') {
        unset($form["default_value"]);
      }
    }
  }
}

/**
 * Implements hook_theme().
 */
function bg_img_field_theme($existing, $type, $theme, $path) {
  return [
    'background_style' => [
      'variables' => [
        'css' => NULL,
      ],
    ],
  ];
}

Functions