You are here

asset.filters.inc in Asset 7

Assets Input filters logic.

File

includes/asset.filters.inc
View source
<?php

/**
 * @file
 * Assets Input filters logic.
 */

/**
 * Renders asset with overridden optional fields.
 */
function assets_render_by_tag($asset, $params_string, $in_editor = FALSE) {
  if (!empty($asset)) {
    $clone = clone $asset;
    $clone->in_editor = $in_editor;
    if (!$in_editor) {

      // Need double decode if ckeditor plugin HTML entities is enabled.
      $params_string = html_entity_decode(html_entity_decode($params_string, ENT_QUOTES, 'UTF-8'));
    }
    $options = json_decode('{' . $params_string . '}', TRUE);
    $view_mode = ASSET_DEFAULT_MODE;
    if (is_array($options)) {
      if (isset($options['mode'])) {
        $view_mode = $options['mode'];
      }
      if (isset($options['align'])) {
        $clone->align = $options['align'];
      }
      $values = array_diff_key($options, array(
        'mode' => 1,
        'align' => 1,
      ));
      _assets_set_field_value($clone, $values);
    }
    $clone->asset_options = $options;
    $clone->asset_source_string = $params_string;

    // As render works by reference we should pass variable instead of direct value.
    $clone_content = $clone
      ->view($view_mode);

    // trim() here is very important. Without trim,
    // if template started with space - dom structure of response will contains empty text node.
    return trim(render($clone_content));
  }
  return '';
}

/**
 * Helper function to keep the initial regexp only here, returns a set of matches per asset tag.
 */
function assets_filter_get_matches($text) {
  $matches = array();
  preg_match_all('/\\[\\[asset:([_a-zA-Z0-9]+):([0-9]+)\\s\\{((\\n|.)*?)\\}\\]\\]/s', $text, $matches, PREG_SET_ORDER);
  return $matches;
}

/**
 * Filter callback for assets markup filter.
 */
function assets_filter_process($text) {
  $text = preg_replace_callback('/\\[\\[asset:([_a-zA-Z0-9]+):([0-9]+)\\s\\{((\\n|.)*?)\\}\\]\\]/s', 'assets_filter_replace_process', $text);
  return $text;
}

/**
 * Filter callback for remove assets filter.
 */
function assets_cut_filter_process($text) {
  $matches = assets_filter_get_matches($text);
  $tags = array();
  foreach ($matches as $match) {
    $tags[] = $match[0];
  }
  if (count($tags)) {
    $text = str_replace($tags, '', $text);
  }
  return $text;
}

/**
 * Filter callback for assets_filter_process.
 */
function assets_filter_replace_process($matches) {
  if (!empty($matches[2]) && !empty($matches[1]) && !empty($matches[3])) {
    $asset = asset_load($matches[2]);
    $rendered = assets_render_by_tag($asset, $matches[3]);
    return $rendered;
  }
  return $matches[0];
}

/**
 * Helper function to override the optional asset field values.
 */
function _assets_set_field_value(&$asset, $values) {
  $types = _assets_get_overridable_field_types();
  foreach ($values as $field_name => $value) {
    $field_info = field_info_field($field_name);
    if (!isset($types[$field_info['type']])) {
      continue;
    }

    // Re-fill fields with overridden values to remove extra deltas.
    $asset->{$field_name} = [];
    $columns = $types[$field_info['type']];
    $langcode = field_language('asset', $asset, $field_name);
    if (!$langcode) {
      $langcode = LANGUAGE_NONE;
    }
    foreach ($value as $delta => $item_value) {
      if (count($columns) > 1) {
        if (is_array($item_value)) {
          foreach ($columns as $column) {
            $asset->{$field_name}[$langcode][$delta][$column] = $item_value[$column];
          }
        }
        else {
          $column = reset($columns);
          $asset->{$field_name}[$langcode][$delta][$column] = $item_value;
          $asset->{$field_name}[$langcode][$delta]['format'] = NULL;
        }
      }
      else {
        $column = reset($columns);
        $asset->{$field_name}[$langcode][$delta][$column] = $item_value;
      }
    }
  }
}

Functions

Namesort descending Description
assets_cut_filter_process Filter callback for remove assets filter.
assets_filter_get_matches Helper function to keep the initial regexp only here, returns a set of matches per asset tag.
assets_filter_process Filter callback for assets markup filter.
assets_filter_replace_process Filter callback for assets_filter_process.
assets_render_by_tag Renders asset with overridden optional fields.
_assets_set_field_value Helper function to override the optional asset field values.