Commit 3cf6ad7c authored by webchick's avatar webchick

Issue #1779026 by dagmar, tim.plunkett, sun, heyrocker: Convert Text Formats...

Issue #1779026 by dagmar, tim.plunkett, sun, heyrocker: Convert Text Formats to Configuration System.
parent 1415a436
......@@ -36,12 +36,11 @@ function setUp() {
config('system.site')->set('page.front', 'test-page')->save();
// Create Full HTML text format.
$full_html_format = array(
$full_html_format = entity_create('filter_format', array(
'format' => 'full_html',
'name' => 'Full HTML',
);
$full_html_format = (object) $full_html_format;
filter_format_save($full_html_format);
));
$full_html_format->save();
$this->checkPermissions(array(), TRUE);
// Create and log in an administrative user having access to the Full HTML
......
......@@ -125,7 +125,8 @@ function testSimpleEntityType() {
}
function testEditorWithCustomMetadata() {
$this->enableModules(array('filter'));
$this->installSchema('system', 'url_alias');
$this->enableModules(array('user', 'filter'));
// Enable edit_test module so that the WYSIWYG Create.js PropertyEditor
// widget becomes available.
......@@ -147,16 +148,15 @@ function testEditorWithCustomMetadata() {
);
// Create a text format.
$full_html_format = array(
$full_html_format = entity_create('filter_format', array(
'format' => 'full_html',
'name' => 'Full HTML',
'weight' => 1,
'filters' => array(
'filter_htmlcorrector' => array('status' => 1),
),
);
$full_html_format = (object) $full_html_format;
filter_format_save($full_html_format);
));
$full_html_format->save();
// Create an entity with values for this rich text field.
$this->entity = field_test_create_entity();
......
......@@ -32,15 +32,14 @@ public static function getInfo() {
function setUp() {
parent::setUp();
// Add text format.
$filtered_html_format = array(
// Add text format.
$filtered_html_format = entity_create('filter_format', array(
'format' => 'filtered_html',
'name' => 'Filtered HTML',
'weight' => 0,
'filters' => array(),
);
$filtered_html_format = (object) $filtered_html_format;
filter_format_save($filtered_html_format);
));
$filtered_html_format->save();
// Create admin user.
$this->admin_user = $this->drupalCreateUser(array('administer filters'));
......
......@@ -32,23 +32,21 @@ public static function getInfo() {
function setUp() {
parent::setUp();
// Add text formats.
$filtered_html_format = array(
// Add text formats.
$filtered_html_format = entity_create('filter_format', array(
'format' => 'filtered_html',
'name' => 'Filtered HTML',
'weight' => 0,
'filters' => array(),
);
$filtered_html_format = (object) $filtered_html_format;
filter_format_save($filtered_html_format);
$full_html_format = array(
));
$filtered_html_format->save();
$full_html_format = entity_create('filter_format', array(
'format' => 'full_html',
'name' => 'Full HTML',
'weight' => 1,
'filters' => array(),
);
$full_html_format = (object) $full_html_format;
filter_format_save($full_html_format);
));
$full_html_format->save();
// Create node type.
$this->drupalCreateContentType(array(
......
......@@ -21,7 +21,7 @@ class EditorManagerTest extends DrupalUnitTestBase {
*
* @var array
*/
public static $modules = array('filter', 'editor');
public static $modules = array('system', 'editor');
/**
* The manager for text editor plugins.
......@@ -42,25 +42,24 @@ function setUp() {
parent::setUp();
// Install the Filter module.
$this->enableModules(array('filter'));
$this->installSchema('system', 'url_alias');
$this->enableModules(array('user', 'filter'));
// Add text formats.
$filtered_html_format = array(
$filtered_html_format = entity_create('filter_format', array(
'format' => 'filtered_html',
'name' => 'Filtered HTML',
'weight' => 0,
'filters' => array(),
);
$filtered_html_format = (object) $filtered_html_format;
filter_format_save($filtered_html_format);
$full_html_format = array(
));
$filtered_html_format->save();
$full_html_format = entity_create('filter_format', array(
'format' => 'full_html',
'name' => 'Full HTML',
'weight' => 1,
'filters' => array(),
);
$full_html_format = (object) $full_html_format;
filter_format_save($full_html_format);
));
$full_html_format->save();
}
/**
......
# Every site requires at least one text format as fallback format that
# - is accessible to all users.
# - is secure, using very basic formatting only.
# - may be modified by installation profiles to have other properties.
format: plain_text
name: 'Plain text'
status: '1'
weight: '10'
roles:
- anonymous
- authenticated
cache: '1'
filters:
# Escape all HTML.
filter_html_escape:
module: filter
status: '1'
# Convert URLs into links.
filter_url:
module: filter
status: '1'
# Convert linebreaks into paragraphs.
filter_autop:
module: filter
status: '1'
langcode: und
......@@ -75,13 +75,12 @@ function filter_admin_overview($form) {
* Form submission handler for filter_admin_overview().
*/
function filter_admin_overview_submit($form, &$form_state) {
$filter_formats = filter_formats();
foreach ($form_state['values']['formats'] as $id => $data) {
// Only update if this is a form element with weight.
if (is_array($data) && isset($data['weight'])) {
// Only update if this is a form element with weight.
db_update('filter_format')
->fields(array('weight' => $data['weight']))
->condition('format', $id)
->execute();
$filter_formats[$id]->set('weight', $data['weight']);
$filter_formats[$id]->save();
}
}
filter_formats_reset();
......@@ -113,10 +112,8 @@ function filter_admin_overview_submit($form, &$form_state) {
function filter_admin_format_page($format = NULL) {
if (!isset($format->name)) {
drupal_set_title(t('Add text format'));
$format = (object) array(
'format' => NULL,
'name' => '',
);
$format = entity_create('filter_format', array());
}
return drupal_get_form('filter_admin_format_form', $format);
}
......@@ -216,6 +213,10 @@ function filter_admin_format_form($form, &$form_state, $format) {
'#title' => t('Enabled filters'),
'#prefix' => '<div id="filters-status-wrapper">',
'#suffix' => '</div>',
// This item is used as a pure wrapping container with heading. Ignore its
// value, since 'filters' should only contain filter definitions.
// @see http://drupal.org/node/1829202
'#input' => FALSE,
);
foreach ($filter_info as $name => $filter) {
$form['filters']['status'][$name] = array(
......@@ -233,6 +234,10 @@ function filter_admin_format_form($form, &$form_state, $format) {
'#type' => 'item',
'#title' => t('Filter processing order'),
'#theme' => 'filter_admin_format_filter_order',
// This item is used as a pure wrapping container with heading. Ignore its
// value, since 'filters' should only contain filter definitions.
// @see http://drupal.org/node/1829202
'#input' => FALSE,
);
foreach ($filter_info as $name => $filter) {
$form['filters']['order'][$name]['filter'] = array(
......@@ -260,8 +265,7 @@ function filter_admin_format_form($form, &$form_state, $format) {
$function = $filter['settings callback'];
// Pass along stored filter settings and default settings, but also the
// format object and all filters to allow for complex implementations.
$defaults = (isset($filter['default settings']) ? $filter['default settings'] : array());
$settings_form = $function($form, $form_state, $filters[$name], $format, $defaults, $filters);
$settings_form = $function($form, $form_state, $filters[$name], $format, $filter['default settings'], $filters);
if (!empty($settings_form)) {
$form['filters']['settings'][$name] = array(
'#type' => 'details',
......@@ -325,9 +329,12 @@ function filter_admin_format_form_validate($form, &$form_state) {
form_set_value($form['format'], $format_format, $form_state);
form_set_value($form['name'], $format_name, $form_state);
$result = db_query("SELECT format FROM {filter_format} WHERE name = :name AND format <> :format", array(':name' => $format_name, ':format' => $format_format))->fetchField();
if ($result) {
form_set_error('name', t('Text format names must be unique. A format named %name already exists.', array('%name' => $format_name)));
$filter_formats = entity_load_multiple('filter_format');
foreach ($filter_formats as $format) {
if ($format->name == $format_name && $format->format != $format_format) {
form_set_error('name', t('Text format names must be unique. A format named %name already exists.', array('%name' => $format_name)));
break;
}
}
}
......@@ -343,13 +350,13 @@ function filter_admin_format_form_submit($form, &$form_state) {
// Add the submitted form values to the text format, and save it.
$format = $form['#format'];
foreach ($form_state['values'] as $key => $value) {
$format->$key = $value;
$format->set($key, $value);
}
$status = filter_format_save($format);
$status = $format->save();
// Save user permissions.
if ($permission = filter_permission_name($format)) {
foreach ($format->roles as $rid => $enabled) {
foreach ($form_state['values']['roles'] as $rid => $enabled) {
user_role_change_permissions($rid, array($permission => $enabled));
}
}
......
......@@ -276,44 +276,11 @@ function hook_filter_FILTER_tips($filter, $format, $long) {
* @{
*/
/**
* Perform actions when a new text format has been created.
*
* @param $format
* The format object of the format being updated.
*
* @see hook_filter_format_update()
* @see hook_filter_format_disable()
*/
function hook_filter_format_insert($format) {
mymodule_cache_rebuild();
}
/**
* Perform actions when a text format has been updated.
*
* This hook allows modules to act when a text format has been updated in any
* way. For example, when filters have been reconfigured, disabled, or
* re-arranged in the text format.
*
* @param $format
* The format object of the format being updated.
*
* @see hook_filter_format_insert()
* @see hook_filter_format_disable()
*/
function hook_filter_format_update($format) {
mymodule_cache_rebuild();
}
/**
* Perform actions when a text format has been disabled.
*
* @param $format
* The format object of the format being disabled.
*
* @see hook_filter_format_insert()
* @see hook_filter_format_update()
*/
function hook_filter_format_disable($format) {
mymodule_cache_rebuild();
......
......@@ -5,146 +5,18 @@
* Install, update, and uninstall functions for the Filter module.
*/
use Drupal\Component\Uuid\Uuid;
/**
* Implements hook_schema().
*/
function filter_schema() {
$schema['filter'] = array(
'description' => 'Table that maps filters (HTML corrector) to text formats (Filtered HTML).',
'fields' => array(
'format' => array(
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'description' => 'Foreign key: The {filter_format}.format to which this filter is assigned.',
),
'module' => array(
'type' => 'varchar',
'length' => 64,
'not null' => TRUE,
'default' => '',
'description' => 'The origin module of the filter.',
),
'name' => array(
'type' => 'varchar',
'length' => 32,
'not null' => TRUE,
'default' => '',
'description' => 'Name of the filter being referenced.',
),
'weight' => array(
'type' => 'int',
'not null' => TRUE,
'default' => 0,
'description' => 'Weight of filter within format.',
),
'status' => array(
'type' => 'int',
'not null' => TRUE,
'default' => 0,
'description' => 'Filter enabled status. (1 = enabled, 0 = disabled)',
),
'settings' => array(
'type' => 'blob',
'not null' => FALSE,
'size' => 'big',
'serialize' => TRUE,
'description' => 'A serialized array of name value pairs that store the filter settings for the specific format.',
),
),
'primary key' => array('format', 'name'),
'indexes' => array(
'list' => array('weight', 'module', 'name'),
),
);
$schema['filter_format'] = array(
'description' => 'Stores text formats: custom groupings of filters, such as Filtered HTML.',
'fields' => array(
'format' => array(
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'description' => 'Primary Key: Unique machine name of the format.',
),
'name' => array(
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => '',
'description' => 'Name of the text format (Filtered HTML).',
'translatable' => TRUE,
),
'cache' => array(
'type' => 'int',
'not null' => TRUE,
'default' => 0,
'size' => 'tiny',
'description' => 'Flag to indicate whether format is cacheable. (1 = cacheable, 0 = not cacheable)',
),
'status' => array(
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 1,
'size' => 'tiny',
'description' => 'The status of the text format. (1 = enabled, 0 = disabled)',
),
'weight' => array(
'type' => 'int',
'not null' => TRUE,
'default' => 0,
'description' => 'Weight of text format to use when listing.',
),
),
'primary key' => array('format'),
'unique keys' => array(
'name' => array('name'),
),
'indexes' => array(
'status_weight' => array('status', 'weight'),
),
);
$schema['cache_filter'] = drupal_get_schema_unprocessed('system', 'cache');
$schema['cache_filter']['description'] = 'Cache table for the Filter module to store already filtered pieces of text, identified by text format and hash of the text.';
return $schema;
}
/**
* Implements hook_install().
*/
function filter_install() {
// All sites require at least one text format (the fallback format) that all
// users have access to, so add it here. We initialize it as a simple, safe
// plain text format with very basic formatting, but it can be modified by
// installation profiles to have other properties.
$plain_text_format = array(
'format' => 'plain_text',
'name' => 'Plain text',
'weight' => 10,
'filters' => array(
// Escape all HTML.
'filter_html_escape' => array(
'weight' => 0,
'status' => 1,
),
// URL filter.
'filter_url' => array(
'weight' => 1,
'status' => 1,
),
// Line break filter.
'filter_autop' => array(
'weight' => 2,
'status' => 1,
),
),
);
$plain_text_format = (object) $plain_text_format;
filter_format_save($plain_text_format);
}
/**
* @addtogroup updates-7.x-to-8.x
* @{
......@@ -161,6 +33,42 @@ function filter_update_8000() {
));
}
/**
* Migrate filter formats into configuration.
*
* @ingroup config_upgrade
*/
function filter_update_8001() {
$uuid = new Uuid();
$result = db_query('SELECT format, name, cache, status, weight FROM {filter_format}', array(), array('fetch' => PDO::FETCH_ASSOC));
foreach ($result as $format) {
$id = $format['format'];
// Generate a UUID.
$format['uuid'] = $uuid->generate();
// Add user roles.
$format['roles'] = array_keys(user_roles(FALSE, 'use text format ' . $format['format']));
// Retrieve and prepare all filters.
$filters = db_query('SELECT name, module, status, weight, settings FROM {filter} WHERE format = :format ORDER BY weight, module, name', array(
':format' => $id,
), array('fetch' => PDO::FETCH_ASSOC))->fetchAllAssoc('name');
foreach ($filters as $name => &$filter) {
// The filter name is used as key only.
unset($filter['name']);
$filter['settings'] = unserialize($filter['settings']);
}
$format['filters'] = $filters;
// Save the config object.
$config = config('filter.format.' . $id);
$config->setData($format);
$config->save();
update_config_manifest_add('filter.format', array($id));
}
}
/**
* @} End of "defgroup updates-7.x-to-8.x".
* The next series of updates should start at 9000.
......
......@@ -7,6 +7,7 @@
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Template\Attribute;
use Drupal\filter\Plugin\Core\Entity\FilterFormat;
/**
* Non-HTML markup language filters that generate HTML.
......@@ -204,116 +205,6 @@ function filter_format_load($format_id) {
return isset($formats[$format_id]) ? $formats[$format_id] : FALSE;
}
/**
* Saves a text format object to the database.
*
* @param $format
* A format object having the properties:
* - format: A machine-readable name representing the ID of the text format
* to save. If this corresponds to an existing text format, that format
* will be updated; otherwise, a new format will be created.
* - name: The title of the text format.
* - status: (optional) An integer indicating whether the text format is
* enabled (1) or not (0). Defaults to 1.
* - weight: (optional) The weight of the text format, which controls its
* placement in text format lists. If omitted, the weight is set to 0.
* - filters: (optional) An associative, multi-dimensional array of filters
* assigned to the text format, keyed by the name of each filter and using
* the properties:
* - weight: (optional) The weight of the filter in the text format. If
* omitted, either the currently stored weight is retained (if there is
* one), or the filter is assigned a weight of 10, which will usually
* put it at the bottom of the list.
* - status: (optional) A Boolean indicating whether the filter is
* enabled in the text format. If omitted, the filter will be disabled.
* - settings: (optional) An array of configured settings for the filter.
* See hook_filter_info() for details.
*
* @return
* SAVED_NEW or SAVED_UPDATED.
*/
function filter_format_save($format) {
$format->name = trim($format->name);
$format->cache = _filter_format_is_cacheable($format);
if (!isset($format->status)) {
$format->status = 1;
}
if (!isset($format->weight)) {
$format->weight = 0;
}
// Insert or update the text format.
$return = db_merge('filter_format')
->key(array('format' => $format->format))
->fields(array(
'name' => $format->name,
'cache' => (int) $format->cache,
'status' => (int) $format->status,
'weight' => (int) $format->weight,
))
->execute();
// Programmatic saves may not contain any filters.
if (!isset($format->filters)) {
$format->filters = array();
}
$filter_info = filter_get_filters();
foreach ($filter_info as $name => $filter) {
// If the format does not specify an explicit weight for a filter, assign
// a default weight, either defined in hook_filter_info(), or the default of
// 0 by filter_get_filters().
if (!isset($format->filters[$name]['weight'])) {
$format->filters[$name]['weight'] = $filter['weight'];
}
$format->filters[$name]['status'] = isset($format->filters[$name]['status']) ? $format->filters[$name]['status'] : 0;
$format->filters[$name]['module'] = $filter['module'];
// If settings were passed, only ensure default settings.
if (isset($format->filters[$name]['settings'])) {
if (isset($filter['default settings'])) {
$format->filters[$name]['settings'] = array_merge($filter['default settings'], $format->filters[$name]['settings']);
}
}
// Otherwise, use default settings or fall back to an empty array.
else {
$format->filters[$name]['settings'] = isset($filter['default settings']) ? $filter['default settings'] : array();
}
$fields = array();
$fields['weight'] = $format->filters[$name]['weight'];
$fields['status'] = $format->filters[$name]['status'];
$fields['module'] = $format->filters[$name]['module'];
$fields['settings'] = serialize($format->filters[$name]['settings']);
db_merge('filter')
->key(array(
'format' => $format->format,
'name' => $name,
))
->fields($fields)
->execute();
}
if ($return == SAVED_NEW) {
module_invoke_all('filter_format_insert', $format);
}
else {
module_invoke_all('filter_format_update', $format);
// Explicitly indicate that the format was updated. We need to do this
// since if the filters were updated but the format object itself was not,
// the merge query above would not return an indication that anything had
// changed.
$return = SAVED_UPDATED;
// Clear the filter cache whenever a text format is updated.
cache('filter')->deleteTags(array('filter_format' => $format->format));
}
filter_formats_reset();
return $return;
}
/**
* Disables a text format.
*
......@@ -326,10 +217,8 @@ function filter_format_save($format) {
* The text format object to be disabled.
*/
function filter_format_disable($format) {
db_update('filter_format')
->fields(array('status' => 0))
->condition('format', $format->format)
->execute();
$format->status = 0;
$format->save();
// Allow modules to react on text format deletion.
module_invoke_all('filter_format_disable', $format);
......@@ -353,7 +242,7 @@ function filter_format_disable($format) {
* @see filter_format_load()
*/
function filter_format_exists($format_id) {
return (bool) db_query_range('SELECT 1 FROM {filter_format} WHERE format = :format', 0, 1, array(':format' => $format_id))->fetchField();
return entity_load('filter_format', $format_id);
}
/**
......@@ -459,13 +348,14 @@ function filter_formats($account = NULL) {
$formats['all'] = $cache->data;
}
else {
$formats['all'] = db_select('filter_format', 'ff')
->addTag('translatable')
->fields('ff')
->condition('status', 1)
->orderBy('weight')
->execute()
->fetchAllAssoc('format');
$filter_formats = entity_load_multiple('filter_format');
$formats['all'] = array();
foreach ($filter_formats as $format_name => $filter_format) {
if (!empty($filter_format->status)) {
$formats['all'][$format_name] = $filter_format;
}
}
uasort($formats['all'], 'Drupal\Core\Config\Entity\ConfigEntityBase::sort');
cache()->set("filter_formats:{$language_interface->langcode}", $formats['all'], CacheBackendInterface::CACHE_PERMANENT, array('filter_formats' => TRUE));
}
......@@ -676,6 +566,7 @@ function filter_get_filters() {
$info[$name] += array(
'description' => '',
'weight' => 0,
'default settings' => array(),
);
}
$filters = array_merge($filters, $info);
......@@ -730,7 +621,7 @@ function filter_format_allowcache($format_id) {
* TRUE if all the filters enabled in the given text format allow caching,
* FALSE otherwise.
*
* @see filter_format_save()
* @see Drupal\filter\Plugin\Core\Entity\FilterFormat::save()
*/
function _filter_format_is_cacheable($format) {
if (empty($format->filters)) {
......@@ -770,9 +661,18 @@ function filter_list_format($format_id) {
$filters['all'] = $cache->data;
}
else {
$result = db_query('SELECT * FROM {filter} ORDER BY weight, module, name');
foreach ($result as $record) {
$filters['all'][$record->format][$record->name] = $record;
$filter_formats = filter_formats();
foreach ($filter_formats as $filter_format) {
foreach ($filter_format->filters as $filter_name => $filter) {
$filter['name'] = $filter_name;
$filters['all'][$filter_format->format][$filter_name] = $filter;
}
uasort($filters['all'][$filter_format->format], 'Drupal\filter\Plugin\Core\Entity\FilterFormat::sortFilters');
// Convert filters into objects.
// @todo Retain filters as arrays or convert to plugins.
foreach ($filters['all'][$filter_format->format] as $filter_name => $filter) {
$filters['all'][$filter_format->format][$filter_name] = (object) $filter;
}
}
cache()->set('filter_list_format', $filters['all']);
}
......@@ -784,12 +684,10 @@ function filter_list_format($format_id) {
foreach ($filter_map as $name => $filter) {