<?php

/**
 * @file
 * The theme implementation for the entity extra field.
 */

declare(strict_types=1);

use Drupal\entity_extra_field\Entity\EntityExtraFieldInterface;

/**
 * The template entity extra field preprocess.
 *
 * @param array $variables
 *   An array of template variables.
 */
function template_preprocess_entity_extra_field(array &$variables): void {
  $element = $variables['element'];
  $extra_field = $element['#field'];

  $variables['label'] = $element['label'];
  $variables['content'] = $element['content'];
  $variables['attributes']['class'][] = 'extra-field';

  if ($extra_field instanceof EntityExtraFieldInterface) {
    $field_class_name = entity_extra_field_class_string([
      $extra_field->name(),
    ]);
    $variables['attributes']['class'][] = 'extra-field--' . $field_class_name;
    $variables['attributes']['class'][] = 'extra-field--type-' . $extra_field->getFieldTypePluginId();
  }
  $variables['title_attributes']['class'][] = 'extra-field--label';
  $variables['content_attributes']['class'][] = 'extra-field--content';
}

/**
 * Format values into a class string.
 *
 * @param array $values
 *   An array of class values.
 * @param string $delimiter
 *   A space delimiter to use between concatenated classes.
 *
 * @return string
 *   The concatenated class string.
 */
function entity_extra_field_class_string(
  array $values,
  string $delimiter = '-'
): string {
  return implode(
    $delimiter,
    array_map('\Drupal\Component\Utility\Html::getClass', $values)
  );
}
