You are here

function media_wysiwyg_aggregate_alignment in D7 Media 7.4

Same name and namespace in other branches
  1. 7.3 modules/media_wysiwyg/includes/media_wysiwyg.filter.inc \media_wysiwyg_aggregate_alignment()

Find and aggregate alignment in media tag.

Version < 3.0 upgrade. Aggregate and uniform various methods for aligning media to one setting. In the process it removes all 'old' methods, like inline style floats and the align attribute. The final alignment is stored in $tag_info['alignment'] and is returned.

Parameters

array $tag_info: JSON-decoded media macro.

Return value

string The alignment of the media, either 'left', 'center', 'right' or '' if unset.

1 call to media_wysiwyg_aggregate_alignment()
media_wysiwyg_upgrade_token in modules/media_wysiwyg/includes/media_wysiwyg.upgrade.inc
Upgrade a single media token to latest version.

File

modules/media_wysiwyg/includes/media_wysiwyg.upgrade.inc, line 207
Code related to media token upgrades.

Code

function media_wysiwyg_aggregate_alignment(array &$tag_info) {
  $alignment = '';
  if (!isset($tag_info['attributes']) || !is_array($tag_info['attributes'])) {
    return $alignment;
  }
  $attributes =& $tag_info['attributes'];

  // Media alignment handling: Transform alignment using either the 'align' or
  // 'style' attributes to configuration. Inline style has higher priority over
  // the align attribute, so we check the 'align' attribute first.
  if (isset($attributes['align'])) {
    $alignment = $attributes['align'];
    unset($attributes['align']);
  }

  // Extract from inline style (float).
  if (!empty($attributes['style'])) {
    $css_properties = media_wysiwyg_parse_css_declarations($attributes['style']);
    if (isset($css_properties['float'])) {
      $alignment = $css_properties['float'];
      unset($css_properties['float']);
      $attributes['style'] = media_wysiwyg_stringify_css_declarations($css_properties);
      if (!$attributes['style']) {
        unset($attributes['style']);
      }
    }
  }

  // Check for alignment in media alignment classes.
  $alignment_options = array(
    'left',
    'center',
    'right',
  );
  if (!empty($attributes['class'])) {
    $class_was_exploded = FALSE;
    if (!is_array($attributes['class'])) {
      $attributes['class'] = explode(' ', $attributes['class']);
      $class_was_exploded = TRUE;
    }
    $alignment_classes = array_map(function ($item) {
      return 'media-wysiwyg-align-' . $item;
    }, $alignment_options);
    $alignments = array_intersect($attributes['class'], $alignment_classes);
    if ($alignments) {
      sscanf(end($alignments), 'media-wysiwyg-align-%s', $alignment);
      $attributes['class'] = array_diff($attributes['class'], $alignment_classes);
    }
    if ($class_was_exploded) {
      $attributes['class'] = implode(' ', $attributes['class']);
    }
  }

  // The actual setting, if it exists, should triumph all of the above.
  if (!empty($tag_info['alignment'])) {
    $alignment = $tag_info['alignment'];
  }

  // The one place to rule them all:
  $tag_info['alignment'] = in_array($alignment, $alignment_options) ? $alignment : '';
  return $tag_info['alignment'];
}