diff --git a/core/modules/filter/filter.admin.inc b/core/modules/filter/filter.admin.inc index 044fe58..2beb371 100644 --- a/core/modules/filter/filter.admin.inc +++ b/core/modules/filter/filter.admin.inc @@ -133,24 +133,27 @@ function filter_admin_format_page($format = NULL) { /** * Form constructor for the text format add/edit form. * - * @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. - * - cache: An integer indicating whether the text format is cacheable (1) or - * not (0). Defaults to 1. - * - 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. + * @param Drupal\filter\TextFormat $format + * The text format to be edited. * * @see filter_admin_format_form_validate() * @see filter_admin_format_form_submit() * @ingroup forms */ -function filter_admin_format_form($form, &$form_state, $format) { +function filter_admin_format_form($form, &$form_state, TextFormat $format) { + // During initial form build, add the entity to the form state for use + // during form building and processing. During a rebuild, use what is in the + // form state. + if (!isset($form_state['text_format'])) { + if (!isset($format)) { + $format = entity_create('text_format', array()); + } + $form_state['text_format'] = $format; + } + else { + $format = $form_state['text_format']; + } + $is_fallback = ($format->format == filter_fallback_format()); $form['#format'] = $format; @@ -333,8 +336,8 @@ 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) { + $result = entity_load('text_format', $format_name); + if (!empty($result) && $result->format != $format_format) { form_set_error('name', t('Text format names must be unique. A format named %name already exists.', array('%name' => $format_name))); } } @@ -348,11 +351,9 @@ function filter_admin_format_form_submit($form, &$form_state) { // Remove unnecessary values. form_state_values_clean($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 = $form_state['text_format']; + entity_form_submit_build_entity('text_format', $format, $form, $form_state); + $status = filter_format_save($format); // Save user permissions. diff --git a/core/modules/filter/filter.module b/core/modules/filter/filter.module index 6c54fae..de366b5 100644 --- a/core/modules/filter/filter.module +++ b/core/modules/filter/filter.module @@ -222,15 +222,7 @@ function filter_format_save($format) { } // 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(); + $return = $format->save(); // Programmatic saves may not contain any filters. if (!isset($format->filters)) { @@ -258,19 +250,16 @@ function filter_format_save($format) { $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']); + $text_format_filter = entity_load('text_format_filter', $format->format . '.' . $name); - db_merge('filter') - ->key(array( - 'format' => $format->format, - 'name' => $name, - )) - ->fields($fields) - ->execute(); + $text_format_filter->format = $format->format; + $text_format_filter->name = $name; + $text_format_filter->weight = $format->filters[$name]['weight']; + $text_format_filter->status = $format->filters[$name]['status']; + $text_format_filter->module = $format->filters[$name]['module']; + $text_format_filter->settings = $format->filters[$name]['settings']; + + $text_format_filter->save(); } if ($return == SAVED_NEW) { @@ -305,10 +294,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); @@ -332,7 +319,8 @@ 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(); + $text_formats = entity_load_multiple('text_format'); + return !empty($text_formats[$format_id]); } /** @@ -409,6 +397,110 @@ function filter_modules_disabled($modules) { } /** + * Implements MODULE_config_import_create(). + */ +function filter_config_import_create($name, $new_config, $old_config) { + $is_text_format = strpos($name, 'text_format.') !== 0; + $is_filter = strpos($name, 'text_format_filter.') !== 0; + if (!$is_text_format && !$is_filter) { + return FALSE; + } + + if ($is_text_format) { + $text_format = entity_create('text_format', $new_config->get()); + $text_format->save(); + } + else { + $text_format = entity_create('text_format_filter', $new_config->get()); + $text_format->save(); + } + return TRUE; +} + +/** + * Implements MODULE_config_import_change(). + */ +function filter_config_import_change($name, $new_config, $old_config) { + $is_text_format = strpos($name, 'text_format.') !== 0; + $is_filter = strpos($name, 'text_format_filter.') !== 0; + if (!$is_text_format && !$is_filter) { + return FALSE; + } + + if ($is_text_format) { + list(, $id) = explode('.', $name); + $entity = entity_load('text_format', $id); + } + else { + list(, $text_format_id, $filter_id) = explode('.', $name); + $entity = entity_load('text_format_filter', $text_format_id . '.' . $filter_id); + } + + $entity->original = clone $entity; + foreach ($old_config->get() as $property => $value) { + $entity->original->$property = $value; + } + + foreach ($new_config->get() as $property => $value) { + $entity->$property = $value; + } + + $entity->save(); + return TRUE; +} + +/** + * Implements MODULE_config_import_delete(). + */ +function filter_config_import_delete($name, $new_config, $old_config) { + $is_text_format = strpos($name, 'text_format.') !== 0; + $is_filter = strpos($name, 'text_format_filter.') !== 0; + if (!$is_text_format && !$is_filter) { + return FALSE; + } + + if ($is_text_format) { + list(, $id) = explode('.', $name); + entity_delete_multiple('text_format', array($id)); + } + else { + list(, $text_format_id, $filter_id) = explode('.', $name); + $id = $text_format_id . '.' . $filter_id; + entity_delete_multiple('text_format_filter', array($id)); + } + + return TRUE; +} + +/** + * Implements hook_entity_info(). + */ +function filter_entity_info() { + $types['text_format'] = array( + 'label' => 'Text Format', + 'entity class' => 'Drupal\filter\TextFormat', + 'controller class' => 'Drupal\config\ConfigStorageController', + 'config prefix' => 'text_format', + 'entity keys' => array( + 'format' => 'format', + 'uuid' => 'uuid', + ), + ); + $types['text_format_filter'] = array( + 'label' => 'Text Format Filter', + 'entity class' => 'Drupal\filter\Filter', + 'controller class' => 'Drupal\config\ConfigStorageController', + 'config prefix' => 'text_format_filter', + 'entity keys' => array( + 'format' => 'format', + 'uuid' => 'uuid', + ), + ); + return $types; +} + + +/** * Retrieves a list of text formats, ordered by weight. * * @param $account @@ -431,13 +523,7 @@ 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'); + $formats['all'] = entity_load_multiple('text_format'); cache()->set("filter_formats:{$language_interface->langcode}", $formats['all']); } @@ -703,7 +789,7 @@ function filter_list_format($format_id) { $filters['all'] = $cache->data; } else { - $result = db_query('SELECT * FROM {filter} ORDER BY weight, module, name'); + $result = entity_load_multiple('text_format_filter'); foreach ($result as $record) { $filters['all'][$record->format][$record->name] = $record; } diff --git a/core/modules/filter/lib/Drupal/filter/TextFormat.php b/core/modules/filter/lib/Drupal/filter/TextFormat.php new file mode 100644 index 0000000..c49fe90 --- /dev/null +++ b/core/modules/filter/lib/Drupal/filter/TextFormat.php @@ -0,0 +1,53 @@ +