You are here

public function BackgroundImageForm::buildImage in Background Image 8

Same name and namespace in other branches
  1. 2.x src/Form/BackgroundImageForm.php \Drupal\background_image\Form\BackgroundImageForm::buildImage()
  2. 2.0.x src/Form/BackgroundImageForm.php \Drupal\background_image\Form\BackgroundImageForm::buildImage()

Builds the "Image" group.

Parameters

array $group: The group render array, passed by reference.

1 call to BackgroundImageForm::buildImage()
BackgroundImageForm::build in src/Form/BackgroundImageForm.php
Builds the Background Image form.

File

src/Form/BackgroundImageForm.php, line 286

Class

BackgroundImageForm

Namespace

Drupal\background_image\Form

Code

public function buildImage(array &$group) {
  $group['#require'] = $this->required;

  // Label.
  $group['label'] = [
    '#access' => !$this->inlineEntity,
    '#type' => 'item',
    '#title' => $this->backgroundImage
      ->getTypeLabel(TRUE),
  ];

  // Image type.
  $group['image_type'] = [
    '#access' => !$this->required && $this->parent,
    '#type' => 'radios',
    '#title' => $this
      ->t('Image'),
    '#options' => $this
      ->inheritOverrideOptions(),
    '#default_value' => $this
      ->getSubformValue([
      'image_group',
      'image_type',
    ], $this->imageType),
  ];
  if ($this->parent) {
    self::addState($this->subform['image'], 'visible', $group['image_type'], [
      '[data-drupal-selector="%selector"] :input' => [
        'value' => BackgroundImageInterface::NORMAL,
      ],
    ]);
  }

  // Add the image field widget to the group and increase its weight.
  $this->subform['image']['#group'] = 'image_group';
  $this->subform['image']['#weight'] = 11;
  $this->subform['image']['#attached']['library'][] = 'background_image/jscolor.picker';
  $image =& static::findFirstInput($this->subform['image'], FALSE);
  $image['#description'] = $this->required ? $this
    ->t('A background image is required.') : NULL;
  $image['#required'] = $this->required;
  $image['#title'] = $this
    ->t('Background Image');

  // Hide the title if there is a parent.
  if ($this->parent) {
    $image['#title_display'] = 'invisible';
  }

  // Return if there is no image.
  if (!$this->imageFile) {
    return;
  }

  // Background Image specific settings for the image.
  $image['background_image'] = [
    '#type' => 'container',
    '#weight' => 1000,
  ];

  // Calculate preload background color.
  $calculated_preload_background_color = static::getBackgroundImageManager()
    ->colorSampleFile($this->imageFile, '');

  // For some reason, the default value of this is "" not NULL.
  // @todo Fix this somehow?
  $preload_background_color = $this
    ->getSubformValue([
    'image',
    '0',
    'background_image',
    'preload_background_color',
    'value',
  ]);
  if (empty($preload_background_color)) {
    $preload_background_color = $this->backgroundImage
      ->getSettings()
      ->get('preload.background_color') ?: $calculated_preload_background_color;
  }

  // Dark.
  $dark = $this
    ->getSubformValue([
    'image',
    '0',
    'background_image',
    'dark',
  ]);
  if (!isset($dark) || empty($preload_background_color)) {
    $dark = $this->backgroundImage
      ->getSettings()
      ->get('dark');
    if (!isset($dark)) {
      $dark = self::getBackgroundImageManager()
        ->colorIsDark($preload_background_color);
    }
  }
  $dark_description = $this
    ->t('If enabled, the "@base_class-dark" class will be added to the BODY element so your theme can adjust styles accordingly.', [
    '@base_class' => static::getBackgroundImageManager()
      ->getBaseClass(),
  ]);
  $image['background_image']['dark'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Dark Image'),
    '#attributes' => [
      'title' => $dark_description,
    ],
    '#label_attributes' => [
      'title' => $dark_description,
    ],
    '#default_value' => $dark,
  ];

  // Preload background color.
  $image['background_image']['preload_background_color'] = [
    '#type' => 'item',
    '#title' => $this
      ->t('Preload background color'),
    '#description' => $this
      ->t('This color will be used while the background images are still loading.'),
  ];
  $image['background_image']['preload_background_color']['value'] = [
    '#type' => 'textfield',
    '#default_value' => $preload_background_color,
    '#size' => 8,
    '#max_length' => 7,
    '#attributes' => [
      'data-jscolor' => '{hash:true,required:false,uppercase:false}',
      'data-calculated-value' => $calculated_preload_background_color,
      'data-default-value' => $preload_background_color,
      'autocomplete' => 'off',
      'autocorrect' => 'off',
      'autocapitalize' => 'off',
      'spellcheck' => 'false',
      'style' => 'width:100px',
    ],
    '#wrapper_attributes' => [
      'style' => 'display: inline-block',
    ],
  ];
  $image['background_image']['preload_background_color']['calculate'] = [
    '#access' => $calculated_preload_background_color !== $preload_background_color,
    '#type' => 'button',
    '#value' => $this
      ->t('Calculate'),
    '#attributes' => [
      'data-jscolor-value' => 'calculatedValue',
      'title' => $this
        ->t('Calculates the average color of the image.'),
    ],
  ];
  self::addState($image['background_image']['preload_background_color']['calculate'], 'invisible', $image['background_image']['preload_background_color']['value'], [
    '*' => [
      'value' => $calculated_preload_background_color,
    ],
  ]);
  $image['background_image']['preload_background_color']['reset'] = [
    '#type' => 'button',
    '#value' => $this
      ->t('Reset'),
    '#attributes' => [
      'data-jscolor-value' => 'defaultValue',
      'title' => $this
        ->t('Resets to the default value.'),
    ],
  ];
  self::addState($image['background_image']['preload_background_color']['reset'], 'invisible', $image['background_image']['preload_background_color']['value'], [
    '*' => [
      'value' => $preload_background_color,
    ],
  ]);
}