You are here

class OpenGraphMetaDrupalLayer in Open Graph meta tags 6

Same name and namespace in other branches
  1. 7 opengraph_meta.common.inc \OpenGraphMetaDrupalLayer

Drupal compatibility layer.

Abstracts away differences between Drupal versions.

Hierarchy

Expanded class hierarchy of OpenGraphMetaDrupalLayer

File

./opengraph_meta.common.inc, line 436

View source
class OpenGraphMetaDrupalLayer {
  private function __construct() {
  }

  /** Get all available node content types. */
  public static function get_node_types() {
    switch (OPENGRAPH_META_DRUPAL_VERSION) {
      case 6:
        return node_get_types();
        break;
      case 7:
      case 8:
        return node_type_get_types();
        break;
      default:
        self::err('No API for fetching node types found.');
        return array();
    }
  }

  /** Render given META tag to HTML output. */
  public static function render_meta_tag($name, $content) {
    switch (OPENGRAPH_META_DRUPAL_VERSION) {
      case 6:
        drupal_set_html_head("<meta property=\"{$name}\" content=\"{$content}\" />");
        break;
      case 7:
      case 8:
        $element = array(
          '#tag' => 'meta',
          '#attributes' => array(
            'property' => $name,
            'content' => $content,
          ),
        );
        drupal_add_html_head($element, "opengraph_meta_{$name}");
        break;
      default:
        self::err('No API for rendering META tags.');
    }
  }

  /**
   * Update the default og:type value for the given node type.
   *
   * Helper to admin settings form.
   *
   * @static
   * @param $type_id
   * @param $form_values
   * @return void
   */
  public static function update_default_ogtype_for_node_type($type_id, $form_values) {
    switch (OPENGRAPH_META_DRUPAL_VERSION) {
      case 6:
        $cck_content_type_id = OPENGRAPH_META_VAR_CONTENT_TYPE_CCK_ . $type_id;
        if (array_key_exists($cck_content_type_id, $form_values['description_cck'])) {
          variable_set($cck_content_type_id, $form_values['description_cck'][$cck_content_type_id]);
        }
      default:
        $content_type_id = OPENGRAPH_META_VAR_CONTENT_TYPE_ . $type_id;
        if (array_key_exists($content_type_id, $form_values['types'])) {
          variable_set($content_type_id, $form_values['types'][$content_type_id]);
        }
    }
  }

  /**
   * Get contents of node body.
   * @param  $node the node object.
   * @return empty string if no body found.
   */
  public static function get_node_body($node) {
    $body = '';
    switch (OPENGRAPH_META_DRUPAL_VERSION) {
      case 6:

        // check if we have an alternative field from which to grab description.
        $description_cck_field = variable_get(OPENGRAPH_META_VAR_CONTENT_TYPE_CCK_ . $node->type, '');
        if (!empty($description_cck_field) && !empty($node->{$description_cck_field})) {
          $v = $node->{$description_cck_field};
          if (is_array($v[0]) && !empty($v[0]['value'])) {
            $body = $v[0]['value'];
          }
        }
        if (empty($body) && !empty($node->body)) {
          $body = $node->body;
        }
        break;
      default:
        $lang = field_language('node', $node, 'body');
        if (!empty($node) && !empty($node->body) && !empty($node->body[$lang]) && !empty($node->body[$lang]['0']) && !empty($node->body[$lang]['0']['value'])) {
          $body = $node->body[$lang]['0']['value'];
        }
        break;
    }
    return $body;
  }

  /**
   * Harvest images from node's image fields.
   *
   * array_walk_recursive() doesn't give us enough flexibility so we do the recursion manually.
   *
   * @param $resultarray will hold results.
   */
  public static function extract_image_fields($fields, array &$resultarray) {
    $_uri_field = 'uri';
    if (6 == OPENGRAPH_META_DRUPAL_VERSION) {
      $_uri_field = 'filepath';
    }
    if (is_array($fields)) {
      if (!empty($fields['filemime']) && FALSE !== stripos($fields['filemime'], 'image') && !empty($fields[$_uri_field])) {
        $url = $fields[$_uri_field];
        if (7 <= OPENGRAPH_META_DRUPAL_VERSION) {
          $url = image_style_url('thumbnail', $fields[$_uri_field]);
        }
        array_push($resultarray, array(
          'title' => !empty($fields['title']) ? $fields['title'] : $url,
          'alt' => !empty($fields['alt']) ? $fields['alt'] : $url,
          'url' => $url,
        ));
      }
      else {
        foreach ($fields as $cv) {
          self::extract_image_fields($cv, $resultarray);
        }
      }
    }
  }

  /** Get rendered IMG tag for the OGMT node image selector. */
  public static function theme_selector_image($image) {
    $attributes = array(
      'class' => 'opengraph-thumb',
    );
    $abs_path = url(ltrim($image['url'], '/'), array(
      'absolute' => TRUE,
    ));
    switch (OPENGRAPH_META_DRUPAL_VERSION) {
      case 6:
        return theme('image', $abs_path, $image['alt'], $image['title'], array_merge($attributes, array(
          'width' => '32px',
          'height' => '32px',
        )), FALSE);
        break;
      case 7:
      case 8:
        return theme('image', array(
          'path' => $abs_path,
          'alt' => $image['alt'],
          'height' => '60px',
          'attributes' => array_merge($attributes, array(
            'title' => $image['title'],
          )),
        ));
        break;
      default:
        self::err('No API for theming images');
        return '';
    }
  }

  /** Delete OGM tags for given node. */
  public static function delete_tags($nid) {
    switch (OPENGRAPH_META_DRUPAL_VERSION) {
      case 6:
        db_query("DELETE FROM {" . OPENGRAPH_META_TABLE . "} WHERE nid = %d", $nid);
        break;
      default:
        db_query("DELETE FROM {" . OPENGRAPH_META_TABLE . "} WHERE nid = :nid", array(
          ':nid' => $nid,
        ));
    }
  }

  /** Load OGM tags for given node. */
  public static function load_tags($nid) {
    switch (OPENGRAPH_META_DRUPAL_VERSION) {
      case 6:
        return db_fetch_object(db_query("SELECT * FROM {" . OPENGRAPH_META_TABLE . "} WHERE nid = %d", $nid));
        break;
      default:
        $result = db_query("SELECT * FROM {" . OPENGRAPH_META_TABLE . "} WHERE nid = :nid", array(
          ':nid' => $nid,
        ));
        if (0 >= $result
          ->rowCount()) {
          return FALSE;
        }
        return $result
          ->fetchObject();
    }
  }

  /**
   * Call given filter on all modules which implement it.
   *
   * @params $name filter name.
   * @param $value initial value.
   * @param $args additional arguments to pass to each filter.
   * @return final filtered value.
   */
  public static function invoke_filter($name, $value, $args = array()) {
    array_push($args, $value);
    $valueIndex = count($args) - 1;
    foreach (module_implements($name) as $module) {
      $args[$valueIndex] = call_user_func_array($module . '_' . $name, $args);
    }
    return $args[$valueIndex];
  }

  /**
   * Log a watchdog error related to the Drupal compatibility layer.
   * @static
   * @param $msg
   * @return void
   */
  private static function err($msg) {
    watchdog('opengraph_meta', '%class: %msg', array(
      '%class' => __CLASS__,
      '%msg' => $msg,
    ), WATCHDOG_ERROR);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
OpenGraphMetaDrupalLayer::delete_tags public static function Delete OGM tags for given node.
OpenGraphMetaDrupalLayer::err private static function Log a watchdog error related to the Drupal compatibility layer. @static
OpenGraphMetaDrupalLayer::extract_image_fields public static function Harvest images from node's image fields.
OpenGraphMetaDrupalLayer::get_node_body public static function Get contents of node body.
OpenGraphMetaDrupalLayer::get_node_types public static function Get all available node content types.
OpenGraphMetaDrupalLayer::invoke_filter public static function Call given filter on all modules which implement it.
OpenGraphMetaDrupalLayer::load_tags public static function Load OGM tags for given node.
OpenGraphMetaDrupalLayer::render_meta_tag public static function Render given META tag to HTML output.
OpenGraphMetaDrupalLayer::theme_selector_image public static function Get rendered IMG tag for the OGMT node image selector.
OpenGraphMetaDrupalLayer::update_default_ogtype_for_node_type public static function Update the default og:type value for the given node type.
OpenGraphMetaDrupalLayer::__construct private function