You are here

function word_link_node_view in Word Link 8

Same name and namespace in other branches
  1. 7 word_link.module \word_link_node_view()

Implements hook_node_view().

File

./word_link.module, line 134
This module allows users to replace previously defined words to the links.

Code

function word_link_node_view($node, $view_mode, $langcode) {

  // Replace words for every view mode except rss.
  if ($view_mode != 'rss') {
    $types = variable_get('word_link_node_types', array());

    // Check current content type.
    if (!empty($types[$node->type]['fields'])) {

      // Get all words from DB.
      $words = word_link_get_link();
      if (!empty($words)) {
        $css_status = variable_get('word_link_css', 1);
        if ($css_status) {
          drupal_add_css(drupal_get_path('module', 'word_link') . '/css/word_link.css');
        }
        foreach ($types[$node->type]['fields'] as $field_name => $field) {
          if (!is_array($field)) {

            // Check if field has some text.
            if (!empty($node->content[$field]['#items'])) {
              foreach ($node->content[$field]['#items'] as $delta => $item) {

                // If output format is not HTML, then don’t do anything.
                if ($item['format'] != 'plain_text' && !empty($node->content[$field][$delta]['#markup'])) {

                  // Change field value.
                  $node->content[$field][$delta]['#markup'] = word_link_replace_text($words, $node->content[$field][$delta]['#markup'], $node->langcode);
                }
              }
            }
          }
          else {

            // Process field collection.
            foreach ($field as $fc) {
              if (!empty($node->content[$field_name]['#items'])) {
                foreach ($node->content[$field_name]['#items'] as $fc_delta => $fc_item) {
                  if (!empty($node->content[$field_name][$fc_delta]['entity']['field_collection_item'][$fc_item['value']][$fc])) {
                    $fc_field =& $node->content[$field_name][$fc_delta]['entity']['field_collection_item'][$fc_item['value']][$fc];
                    foreach ($fc_field['#items'] as $fc_field_delta => $fc_field_value) {
                      if ($fc_field_value['format'] != 'plain_text' && !empty($fc_field[$fc_field_delta]['#markup'])) {
                        $value = word_link_replace_text($words, $fc_field[$fc_field_delta]['#markup'], $node->langcode);
                        $fc_field[$fc_field_delta]['#markup'] = $value;
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}