<?php

/**
 * @file
 * Bootstrap sub-theme.
 *
 * Place your custom PHP code in this file.
 */

use Drupal\block\Entity\Block;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Link;
use Drupal\Core\Url;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\image\Entity\ImageStyle;
use Drupal\openy_home_branch\Plugin\HomeBranchLibrary\HBLocationFinder;
use Drupal\file\Entity\File;
use Drupal\file\FileInterface;
use Drupal\node\NodeInterface;
use Drupal\paragraphs\Entity\Paragraph;
use Drupal\block\BlockInterface;
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;


/**
 * Implements hook_preprocess_html().
 */
function openy_carnation_preprocess_html(array &$variables) {
  $variables['theme_name'] = \Drupal::service('theme.manager')
    ->getActiveTheme()
    ->getName();

  // Get the current path.
  $current_path = \Drupal::service('path.current')->getPath();
  $internal_path = \Drupal::service('path_alias.manager')->getAliasByPath($current_path);

  // Assign it to body class.
  $variables['attributes']['class'][] = str_replace("/", "", $internal_path);

  // Preprocess the node to find all paragraphs and react on them.
  if (!empty($variables['node_type'])) {
    /** @var \Drupal\node\Entity\Node $node */
    if (($node = \Drupal::routeMatch()
      ->getParameter('node')) || ($node = \Drupal::routeMatch()
      ->getParameter('node_preview'))) {
      $pgs = search_node_paragraphs($node, ['microsites_menu'], 'field_header_content');
      if (!empty($pgs)) {
        $paragraph = $pgs[0];
        // Add class to the body.
        $variables['attributes']['class'][] = 'microsites-menu';
        if ($paragraph->field_prgf_ms_menu_hide_menu->value) {
          $variables['attributes']['class'][] = 'microsites-menu--hide-main-menu';
        }
      }

      if (empty($variables['node'])) {
        $variables['node'] = $node;
      }

      $banner_paragraphs = search_node_paragraphs($node,
      ['gallery', 'banner', 'small_banner', 'banner_fixed'], 'field_header_content');
      $node_type = $node->getType();
      if (in_array($node_type, [
        'class',
        'blog',
        'news',
        'camp',
        'event',
        'facility',
        'branch',
        'program',
        'program_subcategory',
      ]) || (count($banner_paragraphs) > 0)) {
        $variables['body_classes'][] = 'with-banner';
      }
      else {
        $variables['body_classes'][] = 'without-banner';
      }
      // Modify body classes for Branch CT if LB usage is enabled.
      if ($node_type === 'branch'
         && $node->hasField('field_use_layout_builder')
         && $node->field_use_layout_builder->value) {
        // Add specific class to identify that LB logic has been enabled.
        $variables['body_classes'][] = 'page-node-type-branch--use-lb';
        // Replace banner class.
        $variables['body_classes'] = array_map(function ($class) {
          return $class == 'with-banner' ? 'without-banner' : $class;
        }, $variables['body_classes']);

      }
    }
  }
  elseif (\Drupal::service('current_route_match')->getRouteName() == 'entity.taxonomy_term.canonical') {
    $variables['body_classes'][] = 'with-banner';
  }
  else {
    $variables['body_classes'][] = 'without-banner';
  }
}

/**
 * Search_node_paragraphs.
 *
 * NodeInterface $node.
 *
 * @param array $paragraph_bundles
 *   A return type array $paragraph_bundles.
 * @param string $node_field_id
 *   String $node_field_id.
 *
 * @return array
 *   Returns array.
 */
function search_node_paragraphs(NodeInterface $node, array $paragraph_bundles, $node_field_id = '') {
  $found_paragraphs = [];
  $field_ids = [];
  if (empty($node_field_id)) {
    // Get all the entity reference revisions fields.
    $map = \Drupal::service('entity_field.manager')
      ->getFieldMapByFieldType('entity_reference_revisions');

    // Get all fields of the node with paragraphs.
    $entity_type_id = 'node';
    foreach ($map[$entity_type_id] as $name => $data) {
      $target_type = FieldStorageConfig::loadByName($entity_type_id, $name)
        ->getSetting('target_type');

      if ($target_type == 'paragraph' && $node->hasField($name)) {
        $field_ids[] = $name;
      }
    }
  }
  else {
    $field_ids[] = $node_field_id;
  }

  // Come through all paragraphs of all fields and execute relevant actions.
  foreach ($field_ids as $field_id) {
    if (!$node->hasField($field_id)) {
      continue;
    }
    $field = $node->get($field_id)->getValue();
    foreach ($field as $field_paragraph) {
      /** @var \Drupal\paragraphs\Entity\Paragraph $paragraph */
      $paragraph = Paragraph::load($field_paragraph['target_id']);

      if ($paragraph && (empty($paragraph_bundles) || in_array($paragraph->bundle(), $paragraph_bundles))) {
        $found_paragraphs[] = $paragraph;
      }
    }
  }
  return $found_paragraphs;
}

/**
 * Implements hook_preprocess_node().
 */
function openy_carnation_preprocess_node(&$variables, $hook) {
  $view_mode = $variables['view_mode'];
  $node = $variables['node'];
  if ($node) {
    switch ($node->getType()) {
      // Program pages.
      case 'program':
        if (!$node->get('field_program_image')->isEmpty()) {
          $uri = $node->get('field_program_image')->entity->get('field_media_image')->entity->uri->value;
          $variables['header_image'] = ImageStyle::load('prgf_gallery')
            ->buildUrl($uri);
        }
        break;

      // Sub-Program pages.
      case 'program_subcategory':
        if (!$node->get('field_category_image')->isEmpty()) {
          $uri = $node->get('field_category_image')->entity->get('field_media_image')->entity->uri->value;
          $variables['header_image'] = ImageStyle::load('prgf_gallery')
            ->buildUrl($uri);
        }
        break;

      case 'branch':
        // Check that home branch enabled.
        if (Drupal::hasService('plugin.manager.home_branch_library')) {
          if (!$variables['page']
            // Disable this override in case we use Layout Builder for Locations page.
            || ($node->hasField('field_use_layout_builder')
              && $node->field_use_layout_builder->value)) {
            return;
          }
          // Fix hb_loc_selector_branch_page plugin for Open Y Carnation theme.
          $variables['#attached']['library'][] = 'openy_carnation/hb_loc_selector_branch_page_override';
        }
        break;
    }

    // Add alt attributes to images in subprogram listings.
    if ($view_mode == 'teaser') {
      if ($node->hasField('field_category_image') && !$node->get('field_category_image')
        ->isEmpty()) {
        $node->get('field_category_image')->entity->get('field_media_image')->alt = $node->label();
      }
    }

    // Add default direction link only if the field is empty.
    if (
      in_array($node->getType(), ['branch', 'camp', 'facility', 'location']) &&
      $view_mode == 'teaser' &&
      $node->hasField('field_location_directions')
    ) {
      $directions_title = t("Directions");
      if (!$node->get('field_location_directions')->isEmpty()) {
        $directions_field_url = $node->get('field_location_directions')->first();
        if ($directions_field_url && $directions_field_url->getUrl()->toString() !== "") {
          return;
        }
        if ($directions_field_url->title) {
          $directions_title = $directions_field_url->title;
        }
      }

      $address = $node->get('field_location_address')->first();

      if ($address) {
        $address_array = $address->toArray();
        $location_address = "{$address_array['address_line1']} {$address_array['locality']}, {$address_array['administrative_area']} {$address_array['postal_code']}";
        $directions_url = Url::fromUri('https://www.google.com/maps/dir/', [
          'query' => [
            'api' => 1,
            'destination' => $location_address,
          ],
        ]);

        $link = Link::fromTextAndUrl($directions_title, $directions_url);
        $link = $link->toRenderable();
        $link['#attributes'] = ['target' => ['_blank']];
        $variables['content']['field_location_directions'] = $link;
      }
    }
  }
}

/**
 * Implements hook_preprocess_HOOK().
 *
 * Pass block region value to content so this can be used in
 * MYTHEME_theme_suggestions_menu_alter.
 */
function openy_carnation_preprocess_block(&$variables) {
  if (!empty($variables['elements']['#id'])) {
    $block = Block::load($variables['elements']['#id']);
    if ($block instanceof BlockInterface) {
      $region = $block->getRegion();
      $variables['content']['#attributes']['region'] = $region;
    }
  }
}

/**
 * Implements hook_preprocess_HOOK().
 */
function openy_carnation_preprocess_menu(&$variables) {
  $variables['menu_name'] = $variables['menu_name'] ?? "";
  if ($variables['menu_name'] === 'main') {
    $variables['display_search'] = theme_get_setting('display_search_form');

    foreach ($variables['items'] as &$item) {
      // Add data-toggle for closing mobile menu on menu item click.
      $item['attributes']->setAttribute('data-toggle', 'collapse');
      $item['attributes']->setAttribute('data-target', '.mobile-sidebar.collapse.show');
    }
  }
}

/**
 * Set a file usage by the theme.
 *
 * @param \Drupal\file\FileInterface $file
 *   The file to set the usage of.
 */
function openy_carnation_set_theme_file_usage(FileInterface $file) {
  $file_usage = \Drupal::service('file.usage');
  $file_uses = $file_usage->listUsage($file);
  if (empty($file_uses) || !array_key_exists('openy_carnation', $file_uses)) {
    // Set usage.
    $file_usage->add($file, 'openy_carnation', 'theme', 1);
  }
}

/**
 * Implements hook_preprocess_page().
 */
function openy_carnation_preprocess_page(array &$variables) {
  $variables['display_search'] = theme_get_setting('display_search_form');
  $variables['search_key'] = theme_get_setting('search_query_key');
  $search_alias = 'internal:/' . theme_get_setting('search_page_alias');
  $variables['search_results_path'] = Url::fromUri($search_alias)->toString();
  $variables['base_path'] = \Drupal::request()->getSchemeAndHttpHost();
  $variables['site_slogan'] = \Drupal::config('system.site')->get('slogan');
  $variables['#attached']['drupalSettings']['matchheight']['selectors'] = load_matchheight_selectors();

  if (Drupal::hasService('plugin.manager.home_branch_library')) {
    // Fix hb_menu_selector plugin for Open Y Carnation theme.
    $variables['#attached']['library'][] = 'openy_carnation/hb_menu_selector_override';
  }
  // Set title value for page.
  $variables['title'] = $variables['page']['#title'];
  openy_carnation_preprocess_page_logo($variables);
}

/**
 * Returns list of css class selectors to apply jquery.matchheight function to.
 *
 * @return array
 *   A Return type array.
 */
function load_matchheight_selectors() {
  $setting = theme_get_setting('matchheight_css_classes');
  if ($setting) {
    $setting = preg_replace("/\n$/", "", $setting);
    if (is_string($setting)) {
      $css_selectors = explode("\n", $setting);
      if (is_array($css_selectors)) {
        return $css_selectors;
      }
    }
  }
  return [];
}

/**
 * Helper function for preprocess_page.
 *
 * @param string $variables
 *   A string type of variable $variables.
 */
function openy_carnation_preprocess_page_logo(&$variables) {
  if ($colored_logo = openy_carnation_entity_file_uri_by_fid(theme_get_setting('colored_logo'))) {
    $variables['colored_logo_url'] = \Drupal::service('file_url_generator')->generateAbsoluteString($colored_logo);
  }
  else {
    $variables['colored_logo_url'] = \Drupal::service('file_url_generator')->generateAbsoluteString(\Drupal::service('extension.list.theme')->getPath('openy_carnation') . "/dist/img/logo-color.png");
  }
  $variables['logo_url'] = $variables['transparent_logo_url'] = \Drupal::service('file_url_generator')->generateAbsoluteString(theme_get_setting('logo.path'));
  $variables['mobile_logo_url'] = $variables['transparent_logo_url'];

  $is_node_camp_related = FALSE;
  if ($node = \Drupal::routeMatch()
    ->getParameter('node')
  ) {
    if (\Drupal::hasService('openy_loc_camp.camp_service') && $service = \Drupal::service('openy_loc_camp.camp_service')) {
      $is_node_camp_related = $service->nodeHasOrIsCamp($node);
    }
    $node_type = $node->getType();
    if ($node_type == 'program') {
      $variables['logo_url'] = $variables['colored_logo_url'];
    }
  }

  if ($is_node_camp_related) {
    if ($camp_logo_uri = openy_carnation_entity_file_uri_by_fid(theme_get_setting('openy_carnation_camp_section_logo'))) {
      $variables['logo_url'] = \Drupal::service('file_url_generator')->generateAbsoluteString($camp_logo_uri);
    }
    $variables['camp_favicon'] = \Drupal::service('file_url_generator')->generateAbsoluteString(openy_carnation_entity_file_uri_by_fid(theme_get_setting('openy_carnation_camp_favicon')));
  }
  $variables['site_logo_is_svg'] = FALSE;
  if (isset($variables['logo_url'])) {
    $logo_path = $variables['logo_url'];
    // If logo is a SVG lets load it content so we can inline it.
    if (strlen($variables['logo_url']) - strpos($variables['logo_url'], '.svg') === 4) {
      $variables['site_logo_is_svg'] = TRUE;
      $variables['site_logo_svg'] = file_get_contents(DRUPAL_ROOT . parse_url($logo_path, PHP_URL_PATH));
    }
  }
}

/**
 * Implements hook_preprocess_ENTITY_TYPE().
 */
function openy_carnation_preprocess_paragraph(array &$variables) {
  if (!Drupal::hasService('plugin.manager.home_branch_library')) {
    return;
  }
  if (empty($variables['paragraph'])) {
    return;
  }

  if ($variables['paragraph']->getType() == HBLocationFinder::PRGF_TYPE) {
    // Fix theming for hb_location_finder plugin for Open Y Carnation theme.
    $variables['#attached']['library'][] = 'openy_carnation/hb_location_finder_override';
  }
}

/**
 * Implements hook_preprocess_menu_local_task().
 */
function openy_carnation_preprocess_menu_local_task(array &$variables) {
  $link = $variables['element']['#link'];
  $url = $link['url'];
  $options = $url->getOptions();
  $url->setOptions($options + $link['localized_options']);
  $variables['item'] = Link::fromTextAndUrl($link['title'], $url);
}

/**
 * Implements hook_preprocess_input().
 *
 * Adds classes to inputs.
 */
function openy_carnation_preprocess_input(&$variables) {
  if ($variables['element']['#type'] == 'submit') {

    if ($variables['element']['#value'] == t('Preview')) {
      $variables['attributes']['class'][] = 'btn-secondary';
    }

    if (empty(array_intersect([
      'btn-primary',
      'btn-secondary',
      'btn-success',
      'btn-warning',
      'btn-danger',
      'btn-link',
      'btn-info',
    ], $variables['attributes']['class']))) {
      $variables['attributes']['class'][] = 'btn-primary';
    }

    // Add default btn class if it is absent at submit button.
    if (!in_array('btn', $variables['attributes']['class'])) {
      $variables['attributes']['class'][] = 'btn';
    }
  }
  if (
    $variables['element']['#type'] == 'textfield'
    || $variables['element']['#type'] == 'email'
    || $variables['element']['#type'] == 'date'
    || $variables['element']['#type'] == 'password'
  ) {
    $variables['attributes']['class'][] = 'form-control';
  }
}

/**
 * Implements hook_theme_suggestions_HOOK_alter().
 *
 * Provide region based menu suggestions.
 */
function openy_carnation_theme_suggestions_menu_alter(&$suggestions, array $variables) {
  if (isset($variables['attributes']['region'])) {
    $suggestion = 'menu__' . $variables['menu_name'] . '__' . $variables['attributes']['region'];
    // Theme suggestions only allow underscores. We must clean the dashes.
    $suggestions[] = strtr($suggestion, '-', '_');  
  }
}

/**
 * Implements hook_theme_suggestions_HOOK_alter().
 */
function openy_carnation_theme_suggestions_field_alter(array &$suggestions, array $variables) {
  $element = $variables['element'];

  $field_name = $element['#field_name'];
  $view_mode = $element['#view_mode'];
  $entity_type = $element['#entity_type'];
  $bundle = $element['#bundle'];

  $suggestions[] = 'field__' . $field_name . '__' . $view_mode;
  $suggestions[] = 'field__' . $entity_type . '__' . $field_name . '__' . $view_mode;
  $suggestions[] = 'field__' . $entity_type . '__' . $bundle . '__' . $field_name . '__' . $view_mode;
}

/**
 * Implements hook_theme_suggestions_HOOK_alter().
 */
function openy_carnation_theme_suggestions_page_alter(array &$suggestions, array $variables) {
  if (
    in_array('page__user__register', $suggestions)
    || in_array('page__user__register', $suggestions)
    || in_array('page__user__password', $suggestions)
  ) {
    $suggestions[] = 'page__user__login';
  }

  // Adds template suggestions for 40x pages.
  $http_error_suggestions = [
    'system.403' => 'page__403',
    'system.404' => 'page__404',
  ];
  $route_name = \Drupal::routeMatch()->getRouteName();
  if (isset($http_error_suggestions[$route_name])) {
    $suggestions[] = $http_error_suggestions[$route_name];
  }

  // If the status code is 403/404 but the route is not pointing somewhere else
  // that means there is a custom route set. Remove custom template suggestion.
  $request = \Drupal::requestStack()->getCurrentRequest();
  $exception = $request?->attributes->get('exception');

  if ($exception instanceof HttpExceptionInterface && $status_code = $exception?->getStatusCode()) {
    if (in_array($status_code, [403, 404], TRUE) && !isset($http_error_suggestions[$route_name])) {
      // Remove the custom template suggestion for 403/404 errors.
      $suggestions = array_diff($suggestions, ['page__' . $status_code]);
    }
  }
}

/**
 * Implements THEME_preprocess_breadcrumb.
 *
 * Trims long page title breadcrumbs.
 */
function openy_carnation_preprocess_breadcrumb(&$variables) {
  if (isset($variables['breadcrumb'])) {
    $last_item = end($variables['breadcrumb']);
    if (strlen($last_item['text']) > 60) {
      $variables['breadcrumb'][key($variables['breadcrumb'])]['text'] = t('Current page');
    }
  }
}

/**
 * Implements hook_theme_suggestions_HOOK_alter().
 */
function openy_carnation_theme_suggestions_slick_alter(array &$suggestions, array $variables, $hook) {
  $types = [];
  foreach ($variables['element']['#items'] as $item) {
    if (isset($item['slide']['#node']) && $item['slide']['#node'] instanceof NodeInterface) {
      $types[] = 'slick__' . $item['slide']['#node']->bundle();
    }
  }
  $suggestions += array_unique($types);
}

/**
 * Implements hook_theme_suggestions_HOOK_alter().
 */
function openy_carnation_theme_suggestions_radios_alter(array &$suggestions, array $variables, $hook) {
  $suggestions[] = 'radios__' . $variables['element']['#name'];
}

/**
 * Implements hook_theme_suggestions_form_alter().
 */
function openy_carnation_theme_suggestions_form_alter(array &$suggestions, array $variables) {
  if (isset($variables['element']['#form_id'])) {
    array_push($suggestions, 'form__' . $variables['element']['#form_id']);
  }
}

/**
 * Implements hook_theme_suggestions_HOOK_alter().
 */
function openy_carnation_theme_suggestions_views_view_unformatted_alter(array &$suggestions, array $variables) {
  if ($variables['view']->id() != 'taxonomy_term') {
    return;
  }
  if (isset($variables['rows'][0]['#node']) && $variables['rows'][0]['#node'] instanceof NodeInterface) {
    $suggestions[] = 'views_view_unformatted__' . $variables['view']->id() . '__' . $variables['rows'][0]['#node']->getType();
  }
}

/**
 * Implements hook_form_system_theme_settings_alter().
 */
function openy_carnation_form_system_theme_settings_alter(&$form, FormStateInterface $form_state, $form_id = NULL) {

  // Work-around for a core bug affecting admin themes. See issue #943212.
  if (isset($form_id)) {
    return;
  }

  $form['search'] = [
    '#type' => 'details',
    '#title' => t('Search'),
    '#open' => TRUE,
  ];

  $form['search']['display_search_form'] = [
    '#type' => 'checkbox',
    '#title' => t('Display the search form'),
    '#default_value' => theme_get_setting('display_search_form'),
    '#description' => t('Show search form in the header'),
  ];

  $form['logo']['settings']['colored_logo'] = [
    '#type' => 'managed_file',
    '#title' => t('White background logo'),
    '#description' => t('To display a logo in the header area where background is white.</br>Image size suggested 92 x 70 pixels.'),
    '#upload_validators' => [
      'file_validate_extensions' => ['png jpg jpeg gif svg'],
    ],
    '#upload_location' => 'public://',
    '#default_value' => theme_get_setting('colored_logo'),
  ];

  if (($file = openy_carnation_entity_file_by_fid(theme_get_setting('colored_logo'))) !== NULL) {
    openy_carnation_set_theme_file_usage($file);
  }

  openy_carnation_camp_section_settings($form, $form_state);
  openy_carnation_matchheight_settings($form, $form_state);
}

/**
 * Camp section on theme settings page.
 */
function openy_carnation_camp_section_settings(array &$form, FormStateInterface &$form_state) {

  $form['camp_section'] = [
    '#type' => 'details',
    '#title' => t('Camp section'),
    '#description' => t('Contains theme configuration for camp and camp related content.'),
    '#open' => TRUE,
  ];
  $form['camp_section']['openy_carnation_camp_section_logo'] = [
    '#type' => 'managed_file',
    '#title' => t('Camp section logo'),
    '#description' => t('To display a logo in the header area on camp pages, and camp related landing pages, upload it here.</br>Image size suggested 92 x 70 pixels.'),
    '#upload_validators' => [
      'file_validate_extensions' => ['png jpg jpeg gif svg'],
    ],
    '#upload_location' => 'public://',
    '#default_value' => theme_get_setting('openy_carnation_camp_section_logo'),
  ];
  $form['camp_section']['openy_carnation_camp_favicon'] = [
    '#type' => 'managed_file',
    '#title' => t('Camp favicon'),
    '#description' => t('To display a favicon on camp pages, and camp related landing pages, upload it here.</br>Image sizes must be 16×16, 32×32, 48×48, or 64×64 pixels.</br>Accepted file types ico, png, gif.'),
    '#upload_validators' => [
      'file_validate_extensions' => ['ico png gif'],
    ],
    '#upload_location' => 'public://',
    '#default_value' => theme_get_setting('openy_carnation_camp_favicon'),
  ];

  // Submission handler is not working on theme form
  // See https://www.drupal.org/node/1862892 &
  // See https://www.drupal.org/node/2779947
  // Setting file usage for openy_carnation theme.
  if (($file = openy_carnation_entity_file_by_fid(theme_get_setting('openy_carnation_camp_section_logo'))) !== NULL) {
    openy_carnation_set_theme_file_usage($file);
  }
  if (($file = openy_carnation_entity_file_by_fid(theme_get_setting('openy_carnation_camp_favicon'))) !== NULL) {
    openy_carnation_set_theme_file_usage($file);
  }
}

/**
 * Additional form elements for matchheight section.
 *
 * @param array $form
 *   An array type of variable $form.
 * @param \Drupal\Core\Form\FormStateInterface $form_state
 *   FormStateInterface type of Variable $form-state.
 */
function openy_carnation_matchheight_settings(array &$form, FormStateInterface &$form_state) {

  $form['matchheight_section'] = [
    '#type' => 'details',
    '#title' => t('jQuery MatchHeight'),
    '#description' => t('<a href="https://brm.io/jquery-match-height/">MatchHeight</a> makes the height of all selected elements exactly equal. While this used to be a tricky problem, it is advisable to use modern CSS like Flexbox and CSS Grid. These settings will be ignored on pages that use YMCA\'s Layout Builder (via the "y_lb" module).'),
    '#open' => TRUE,
  ];

  $form['matchheight_section']['matchheight_css_classes'] = [
    '#description' => t('CSS selectors for matchheight library'),
    '#type' => 'textarea',
    '#rows' => '10',
    '#cols' => '10',
    '#resizable' => 'vertical',
    '#default_value' => theme_get_setting('matchheight_css_classes'),
  ];
}

/**
 * Helper function that prepare's logo image for rendering.
 *
 * @param string $fid_array
 *   A string containing array.
 *
 * @return \Drupal\file\Entity\File|null
 *   Returns array.
 */
function openy_carnation_entity_file_by_fid($fid_array) {
  if (!empty($fid_array) && is_array($fid_array) && is_numeric(reset($fid_array))) {
    $file = File::load(reset($fid_array));
    if ($file !== NULL) {
      return $file;
    }
  }

  return NULL;
}

/**
 * Helper function that generates url to image based on fid.
 *
 * @param string $fid_array
 *   A string containing array.
 *
 * @return uri|
 *   Returns array.
 */
function openy_carnation_entity_file_uri_by_fid($fid_array) {
  $file = openy_carnation_entity_file_by_fid($fid_array);
  if (!empty($file)) {
    return $file->getFileUri();
  }

  return '';
}

/**
 * Implements hook_preprocess_HOOK().
 */
function openy_carnation_preprocess_node__news__teaser(&$variables) {
  $variables['default_image_path'] = \Drupal::service('file_url_generator')
    ->generateAbsoluteString(\Drupal::service('extension.list.theme')->getPath('openy_carnation') . "/logo.svg");
}


/**
 * Implements hook_theme_suggestions_HOOK_alter().
 */
function openy_carnation_theme_suggestions_input_alter(array &$suggestions, array $variables) {
  if ($variables['element']['#type'] === 'hidden' && $variables['element']['#attributes']['data-drupal-selector'] === 'edit-upload-uploaded-files') {
    $suggestions[] = 'input__hidden__upload';
  }
}
