You are here

easymeta.module in Easymeta 8

Contains easy_meta.module.

File

easymeta.module
View source
<?php

/**
 * @file
 * Contains easy_meta.module.
 */
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\easymeta\Meta;

/**
 * Implements hook_page_bottom().
 */
function easymeta_page_bottom(array &$page_bottom) {
  if (!\Drupal::currentUser()
    ->hasPermission('manage easy meta')) {
    return;
  }
  $config = \Drupal::config('easymeta.metas');
  $form = \Drupal::formBuilder()
    ->getForm('Drupal\\easymeta\\Form\\EasyMetaForm', $config);
  $page_bottom['easymeta'] = $form;
}

/**
 * Implements hook_page_attachments().
 */
function easymeta_page_attachments(array &$page) {
  $current_path = \Drupal::service('path.current')
    ->getPath();
  $current_language = \Drupal::languageManager()
    ->getCurrentLanguage()
    ->getId();
  $meta = new Meta($current_language, $current_path);
  $meta_values = $meta
    ->getValue();
  if ($meta_values) {
    foreach ($meta_values as $meta_value) {
      $meta_tag = NULL;
      if (isset($meta_value['tag']) && isset($meta_value['value']) && !empty($meta_value['value'])) {
        $meta_tag['#tag'] = $meta_value['tag'];
        if (isset($meta_value['name_property'])) {
          $meta_tag['#attributes']['name'] = $meta_value['name_property'];
        }
        if (isset($meta_value['property'])) {
          $meta_tag['#attributes']['property'] = $meta_value['property'];
        }
        if ($meta_value['name'] != "og_image") {
          $meta_tag['#attributes']['content'] = $meta_value['value'];
        }
        else {
          $url = NULL;
          if (isset($meta_value['value'][0])) {
            $file = file_load($meta_value['value'][0]);
            if ($file) {
              $url = file_create_url($file
                ->getFileUri());
            }
          }
          $meta_tag['#attributes']['content'] = $url;
        }
        $page['#attached']['html_head'][] = [
          $meta_tag,
          $meta_value['name'],
        ];
      }
    }
  }
}

/**
 * Implements hook_preprocess_html().
 */
function easymeta_preprocess_html(&$variables) {
  $current_path = \Drupal::service('path.current')
    ->getPath();
  $current_language = \Drupal::languageManager()
    ->getCurrentLanguage()
    ->getId();
  $meta = new Meta($current_language, $current_path);
  $meta_values = $meta
    ->getValue();
  if (isset($meta_values['title']['value'])) {
    unset($variables['head_title']['name']);
    $variables['head_title']['title'] = $meta_values['title']['value'];
  }
}

/**
 * Implements hook_page_attachments_alter().
 */
function easymeta_page_attachments_alter(array &$page) {
  if (!\Drupal::currentUser()
    ->hasPermission('access contextual links')) {
    return;
  }
  $page['#attached']['library'][] = 'easymeta/easymeta';
}

/**
 * Implements hook_help().
 */
function easymeta_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {

    // Main module help for the easy_meta module.
    case 'help.page.easy_meta':
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('Easy Meta') . '</p>';
      return $output;
    default:
  }
}