You are here

function media_wysiwyg_rebuild_instance_settings in D7 Media 7.4

Rebuild media token based on schema and allowed overridable fields.

This only includes properties of media instance settings that are part of the token schema. Attributes that are fed by fields (usually alt and title) are removed, classes that are generated during input filtering are removed and properties that are empty are removed.

Parameters

array $settings: The media instance settings.

Return value

array The cleaned and rebuilt media settings.

1 call to media_wysiwyg_rebuild_instance_settings()
media_wysiwyg_token_upgrade_40 in modules/media_wysiwyg/includes/media_wysiwyg.upgrade.inc
Upgrade a single media token from 3.0 to 4.0.

File

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

Code

function media_wysiwyg_rebuild_instance_settings(array $settings) {
  $rebuild = array();
  $schema = media_wysiwyg_schema_token();
  foreach ($schema as $property => $prop_settings) {
    if (isset($settings[$property])) {
      if (!empty($settings[$property])) {
        $rebuild[$property] = $settings[$property];
      }
    }
  }

  // Inspect and remove old and generated classes.
  if (!empty($rebuild['attributes']['class'])) {
    $classes = explode(' ', $rebuild['attributes']['class']);
    $classes = array_filter($classes, function ($class) {
      return !($class == 'media-element' || preg_match('/^file-\\S+/', $class) || preg_match('/^media-wysiwyg-align-\\S+/', $class));
    });

    // Old versions of media_wysiwyg added the view mode as its own class name,
    // without prefix. Remove this as well.
    $view_mode_class = strtr($rebuild['view_mode'], '_', '-');
    if (($index = array_search($view_mode_class, $classes)) !== FALSE) {
      unset($classes[$index]);
    }
    if ($classes) {
      $rebuild['attributes']['class'] = implode(' ', $classes);
    }
    else {
      unset($rebuild['attributes']['class']);
    }
  }
  if (!empty($rebuild['attributes'])) {

    // Allowed attributes in older versions of media.
    unset($rebuild['attributes']['data-delta']);
    unset($rebuild['attributes']['data-fid']);
    unset($rebuild['attributes']['data-media-element']);

    // Remove attributes that are fed through file entity fields or media token
    // field overrides.
    foreach (media_wysiwyg_get_attribute_fields() as $attribute => $field_name) {
      unset($rebuild['attributes'][$attribute]);
    }
  }
  if (empty($rebuild['attributes'])) {
    unset($rebuild['attributes']);
  }

  // Move over overridable fields from old $settings to new $rebuild.
  $file_type = db_select('file_managed', 'f')
    ->fields('f', array(
    'type',
  ))
    ->condition('fid', $rebuild['fid'])
    ->execute()
    ->fetchField();
  if ($file_type) {
    $overridable_types = media_wysiwyg_overridable_fields();
    if ($overridables = $overridable_types[$file_type]) {
      foreach ($overridables as $field_name => $status) {
        foreach ($settings as $property => $value) {
          if (strpos($property, $field_name . '[') === 0) {
            $rebuild[$property] = $settings[$property];
          }
        }
      }
    }
  }
  return $rebuild;
}