I'd like to have two text fields in back end and programmatically manipulate the texts in these two fields before displaying the result in the front end. The text fields need to be WYSIWYG/CKeditor - preferably the standard editor.

How would I go around this? Should I create my own custom field or use hooks? Can you provide a not too complex example?


Since version 5, Drupal provides filters, a mechanism to manipulate the value of a text filter before rendering. In Drupal 8, filters are implemented as plugins.

So, in order to have two (text) fields which can can manipulate the value before display, you would need to

  1. Create a custom filter (in a custom module)
  2. Define a text format specific for your two fields, configured to use your custom filter (and any other filter you still want to apply)
  3. On your content type, configure the two fields to use that text format
  4. ?
  5. Profit

You filter code would look like

namespace Drupal\celebrate\Plugin\Filter;

use Drupal\filter\FilterProcessResult;
use Drupal\filter\Plugin\FilterBase;

 * @Filter(
 *   id = "filter_ninjafart",
 *   title = @Translation("NinjaFart Filter"),
 *   type = Drupal\filter\Plugin\FilterInterface::TYPE_MARKUP_LANGUAGE,
 * )
class NinjaFartFilter extends FilterBase {
  public function process($text, $langcode) {
    $text = doSomethingWith($text);
    return new FilterProcessResult($text);
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy