<?php

/**
 * @file
 * File containing d_block module.
 */

use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Template\Attribute;
use Drupal\d_p\Plugin\Field\FieldType\ConfigurationStorage;

/**
 * Implements hook_theme().
 */
function d_block_theme($existing, $type, $theme, $path) {
  return [
    'paragraph__d_p_block' => [
      'base hook' => 'paragraph',
    ],
  ];
}

/**
 * Implements hook_preprocess_paragraph().
 */
function d_block_preprocess_paragraph__d_p_block(&$variables) {
  $variables['content_attributes'] = new Attribute([
    'class' => ['content'],
  ]);

  /** @var \Drupal\d_p\Plugin\Field\ConfigurationStorageFieldItemListInterface $setting_field */
  $setting_field = ConfigurationStorage::getSettingsFieldFromEntity($variables['paragraph']);
  if (!$setting_field->hasClass('full-width')) {
    $variables['content_attributes']->addClass('container');
  }
}

/**
 * Implements hook_preprocess_HOOK().
 */
function d_block_preprocess_field__paragraph__field_block__d_p_block(array &$variables) {
  foreach ($variables['items'] as &$item) {
    $item['content']['#title_attributes'] = [
      'class' => [
        'container',
        'text-center',
      ],
    ];
  }
}

/**
 * Implements hook_preprocess_block().
 */
function d_block_preprocess_block(array &$variables) {
  if (isset($variables['elements']['#title_attributes'])) {
    $variables['title_attributes'] = NestedArray::mergeDeep($variables['title_attributes'], $variables['elements']['#title_attributes']);
  }
}
