You are here

function metatags_quick_page_build in Meta tags quick 8.3

Same name and namespace in other branches
  1. 7.2 metatags_quick.module \metatags_quick_page_build()
  2. 7 metatags_quick.module \metatags_quick_page_build()

Implements hook_field_access().

function metatags_quick_field_access($op, $field, $entity_type, $entity, $account) { if ($field['type'] == 'metatags_quick' && $op != 'view' && !user_access('edit metatags_quick')) { return FALSE; } return TRUE; }

On field load, add meta name to the field data for storage in cache and further rendering

function metatags_quick_field_load($entity_type, $entities, $field, $instances, $langcode, &$items, $age) { foreach ($items as $lang => $lang_item) { foreach ($lang_item as $i => $final_item) { if (!isset($items[$lang][$i]['meta_name'])) { $items[$lang][$i]['meta_name'] = $field['settings']['meta_name']; } } } }

Implements hook_field_insert

function metatags_quick_field_insert($entity_type, $entities, $field, $instances, $langcode, &$items, $age) { // Serialize array items - for the meta robots foreach ($items as $index => $item) { if (is_array($item['metatags_quick'])) { $non_empty = array(); foreach ($item['metatags_quick'] as $subitem) { if ($subitem) { $non_empty[] = $subitem; } } $items[$index]['metatags_quick'] = join(',', $non_empty); } } }

Implements hook_field_update

function metatags_quick_field_update($entity_type, $entities, $field, $instances, $langcode, &$items, $age) { metatags_quick_field_insert($entity_type, $entities, $field, $instances, $langcode, $items, $age); }

Implements hook_page_build().

See also

http://api.drupal.org/api/drupal/modules--field--field.api.php/function/...

http://api.drupal.org/api/drupal/modules--field--field.api.php/function/...

http://api.drupal.org/api/drupal/modules--field--field.api.php/function/...

File

./metatags_quick.module, line 141
Meta tags implemented with FieldAPI/EntityAPI

Code

function metatags_quick_page_build(&$page) {
  $language = \Drupal::languageManager()
    ->getLanguage();
  if (variable_get('metatags_quick_use_path_based', 1) && _metatags_quick_path_based_page()) {
    $current_path = current_path();

    // Try to load path-based meta tags
    $path_based_id = db_select('metatags_quick_path_based', 'pv')
      ->condition('lang', $language->id)
      ->condition('path', $current_path)
      ->fields('pv', array(
      'id',
    ))
      ->execute()
      ->fetchField();

    // if no excact match found - do wildcard search
    if ($path_based_id == 0 && strstr($current_path, "/")) {
      $parts = explode("/", $current_path);

      // iterate through parts
      for ($i = count($parts) - 1; $i > 0; $i--) {

        // create path
        $path = "";
        for ($j = 0; $j < $i; $j++) {
          $path .= $parts[$j] . "/";
        }

        // do wildcard query
        $path_based_id = db_select('metatags_quick_path_based', 'pv')
          ->condition('lang', $language->id)
          ->condition('path', $path . "*")
          ->fields('pv', array(
          'id',
        ))
          ->execute()
          ->fetchField();

        // check for results
        if ($path_based_id > 0) {
          break;
        }
      }
    }
    if ($path_based_id > 0) {
      $controller = new DrupalDefaultEntityController('metatags_path_based');
      $path_entities = $controller
        ->load(array(
        $path_based_id,
      ));
      foreach ($path_entities as $entity) {
        field_attach_view('metatags_path_based', $entity, 'default', LANGUAGE_NONE);
      }
    }
  }
}