This is a follow-up of #2224643: Support for input format configuration on a per-field basis .

Problem/Motivation

When transferring content from one Drupal website to another it can be handy if the source can decide which text format should be used. This idea is originally proposed by hanoii in #2224643: Support for input format configuration on a per-field basis .

Proposed resolution

A new mapper is introduced, which allows one to map to text format directly. As allowing just any format could introduce security issues, there is a mapping configuration option which with you can set the allowed formats (thanks to hanoii by providing this idea in #2224643-18: Support for input format configuration on a per-field basis ). By default, only the default text format is allowed (to reduce the chance that users will accidently insecure their website). The mapper will only be available for text fields that have "Text processing" set to "Filtered text (user selects text format)".

Remaining tasks

User interface changes

A mapper that allows you to map to text format will be available from the list of mappers to choose from if you have text fields that have "Text processing" set to "Filtered text (user selects text format)".

API changes

None.

I'll plan to work on this and hopefully provide a first patch next week.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

MegaChriz’s picture

Status: Active » Needs review
FileSize
12.38 KB

The attached patch adds a target for mapping to text format for a text field.

Mapping to format vs format target configuration

One of the problems that I faced was that the target configuration "format" for the "value" part of text fields could overwrite the mapped value, depending on the order of mappings. Let me illustrate this with an example:

  1. First, you added a mapper that maps to "body:format". You set the formats "Plain text" and "Filtered HTML" to be allowed.
  2. Second, you added a mapper that maps to "body:value". For this mapper, target configuration exists that allows you to set the text format for this target. The format is set to "Plain text".
  3. You import the following CSV file:
    body_format,body_value
        "filtered_html","<em>Lorem Ipsum</em>"

At first, this resulted into the body format becoming "Plain text", because the second mapper also did set the format for the body field.

To solve this problem I added the following code line to keep track of whether or not the mapping to format had taken place:

$entity->feeds_item->format_mapped[$field_name][$delta] = TRUE;

Changes to FeedsWebTestCase::addMappings()

The format target has configuration for setting the formats to accept from the source (this exists to prevent the source from setting any text format, which would be a security issue if, for example, the php text format is available). The config option "allowed_formats" accepts multiple values, but FeedsWebTestCase::addMappings() assumed all target configurations were single-valued, so that's why I had to adjust this method in order for the automated tests to work.

Automated tests

A new test case called "FeedsMapperFormatTestCase" is added. This contains two test methods:

  1. A test for mapping to format
    This also tests if:
    • Mapping to text format works as expected when the mapping for the text field itself comes later (see "Mapping to format vs format target configuration" above).
    • Non-allowed text formats are not accepted.
  2. A test that tests that users with limited privileges can only select text formats they have access to as allowed formats.

The following is not yet covered by the automated tests and may need to be added:

  • Mapping to format is only available for text fields that have "Text processing" set to "Filtered text (user selects text format)".
  • When target option "allowed_formats" is not configured, only the default text format is allowed.

I hope to extend the tests later.

MegaChriz’s picture

Assigned: MegaChriz » Unassigned
FileSize
22.56 KB
18.46 KB

I have completed the automated tests for mapping to text format. I also slightly changed the implementation of the format mapper.

Summary of the changes:

  • The text format mapper no longer specifies a "real target" as I found out that would clear out the "value" of the text field even when not mapping to the text field (but to the text format only).
  • When target configuration for the format target is not configured, both the site's default text format as well as the importer's default text format are allowed.
  • A test has been added that tests the behaviour when using the default target configuration of a format target.
  • A test has been added that tests if the format target only exists for text fields that have filtered text enabled.
  • I added the function text_feeds_format_defaults() to not having to repeat the default target configuration for format targets.

More details about the working of the tests can be found in the code comments.

I think this is ready.

tyler.frankenstein’s picture

In the mean time, it's possible to set the input format for your field using Rules.

Event: Before saving an item imported via [my-feed-importer].
Actions: Set a data value
Parameter: Data: [my-entity-type:field-my-field..., Value: Filtered HTML