0

In my custom module (in Drupal 8), I am using a form field (in a custom form) to use CKEDITOR with the following code which loads the editor fine.

$form['full_detail_text'] = array(
  '#type' => 'text_format',
  '#format' => 'full_html',
  '#title' => t('Full Detail'),
  '#rows' => 5,
);

The following code saves the posted data in the database.

$query = db_insert('my_table')
  ->fields(array(
    'full_detail_text' => $form_state->getValue('english_copy_text')[value],
    'date' => REQUEST_TIME,
  ))
  ->execute();

I am using a view page display to render this output, but the output for this field is displaying data as plain text (no formatting) along with HTML code instead of formatted data same as in the editor.

Is there a configuration setting I need to adjust or perhaps hook_views_pre_render() to transform this field's data from plain text to HTML markup?

2 Answers 2

1

I believe you can use check_markup to render the text in the specified format. In your example, you'd want to also save the format, something like:

$query = db_insert('my_table')
  ->fields(array(
   'full_detail_text' => $form_state->getValue('english_copy_text')[value],
   'full_detail_format' => $form_state->getValue('english_copy_text')[format],
   'date' => REQUEST_TIME,
  ))
 ->execute();

Then when you go to render it in the hook_views_pre_render

$rendered_markup  = check_markup($full_detail_text, $full_detail_format),

For more info, see: https://api.drupal.org/api/drupal/core%21modules%21filter%21filter.module/function/check_markup/8.3.x

1
  • Can you provide a sample code which I should use in hook_views_pre_render()? I have tried but no luck and wondering how this function works. Thanks Commented Aug 16, 2017 at 15:55
0

I have achieved above by not using any hook except following steps.

STEP 1:

$query = db_insert('my_table') ->fields(array( 'full_detail_text' => check_markup($form_state->getValue('full_detail_text')['value'], 'full_html'), 'full_detail_format' => $form_state->getValue('english_copy_text')[format], 'date' => REQUEST_TIME, )) ->execute();

STEP 2:

MY_MODULE.views.ins file...

Add following code to the table.field so as it renders output as markup/html.

'field' => array( // ID of field handler plugin to use. 'id' => 'markup', ),

This solution applies to any html data which should be rendered on view display.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.