You are here

class OpenGraphMetaDrupalLayer in Open Graph meta tags 7

Same name and namespace in other branches
  1. 6 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 432

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

  /** Get all available node content types. */
  public static function get_node_types() {
    return node_type_get_types();
  }

  /** Render given META tag to HTML output. */
  public static function render_meta_tag($name, $content) {
    $element = array(
      '#tag' => 'meta',
      '#attributes' => array(
        'property' => $name,
        'content' => $content,
      ),
    );
    drupal_add_html_head($element, "opengraph_meta_{$name}");
  }

  /**
   * 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) {
    $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 = '';
    $lang = field_language('node', $node, 'body');
    $lang = $lang ? $lang : LANGUAGE_NONE;
    if (!empty($node->body[$lang]['0']['value'])) {
      $body = $node->body[$lang]['0']['value'];
    }
    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) {
    if (is_array($fields)) {
      if (!empty($fields['filemime']) && FALSE !== stripos($fields['filemime'], 'image') && !empty($fields['uri'])) {
        $url = $fields['uri'];
        $thumb_url = image_style_url('thumbnail', $fields['uri']);
        $resultarray[$url] = array(
          'title' => !empty($fields['title']) ? $fields['title'] : $url,
          'alt' => !empty($fields['alt']) ? $fields['alt'] : $url,
          'url' => $thumb_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'], '/'));
    return theme('image', array(
      'path' => $abs_path,
      'alt' => $image['alt'],
      'height' => '60px',
      'attributes' => array_merge($attributes, array(
        'title' => $image['title'],
      )),
    ));
  }

  /** Delete OGM tags for given node. */
  public static function delete_tags($nid) {
    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) {
    $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