govCMS / GovCMS8

Current stable release of the main GovCMS8 distribution.
GNU General Public License v2.0
60 stars 58 forks source link

Add ckeditor_iframe Drupal module to govCMS #318

Open joshua-graham-adelphi opened 5 years ago

joshua-graham-adelphi commented 5 years ago

Some agencies would like to show PDF in-line in the html.

https://www.drupal.org/project/ckeditor_iframe

For now I making testing a hack solution...

/**
 * Implements hook_element_info_alter().
 */
function my_admin_theme_element_info_alter(&$types) {
  $types['text_format']['#pre_render'][] = 'my_admin_theme_form_alter__my_pre_render_text_format';
}

/**
 * Hack to define plugin in the thtme.
 *
 * Until plugins can be defined in the theme layer some how?
 *
 * @see https://www.drupal.org/docs/8/api/plugin-api/creating-a-plugin-that-can-be-defined-in-themes
 */
function my_admin_theme_form_alter__my_pre_render_text_format($element) {
  $theme_path = MyAdminTheme::getTheme('my_admin_theme')->getPath();
  $theme_path .= '/libraries/iframe/plugin.js';
  $theme_path = base_path() . $theme_path;
  //kint($theme_path);

  $plugin_definition = ['class' => 'Drupal\my_admin_theme\Plugin\CKEditorPlugin\FakeObjects'];
  $fake_objects_plugin = new FakeObjects([], 'fakeobjects', $plugin_definition);

  $editor = editor_load('rich_text');
  $plugin_definition = ['class' => 'Drupal\my_admin_theme\Plugin\CKEditorPlugin\IFrame'];
  $iframe_plugin = new IFrame([], 'iframe', $plugin_definition);

  $element['#attached']['drupalSettings']['editor']['formats']['rich_text']['editorSettings']['drupalExternalPlugins']['fakeobjects'] = $fake_objects_plugin->getFile();
  $element['#attached']['drupalSettings']['editor']['formats']['rich_text']['editorSettings']['drupalExternalPlugins']['iframe'] = $iframe_plugin->getFile();
  $element['#attached']['drupalSettings']['editor']['formats']['rich_text']['editorSettings']['extraPlugins'] .= ',fakeobjects,iframe';

  $element['#attached']['drupalSettings']['editor']['formats']['rich_text']['editorSettings']['toolbar'][3]['items'][] = 'Iframe';

  // This should be changed in the UI, just for demonstration.
  $element['#attached']['drupalSettings']['editor']['formats']['rich_text']['editorSettings']['allowedContent']['iframe'] = [
    "attributes" => "longdesc,name,scrolling,src,title,align,height,frameborder,width",
    "styles" => false,
    "classes" => false
  ];
  // Not required to sort by alpha order but druapl does it.
  ksort($element['#attached']['drupalSettings']['editor']['formats']['rich_text']['editorSettings']['allowedContent']);

  return $element;
}
brett-sargeant commented 5 years ago

By display the PDF inline, do you mean the content of the pdf? Would that require an active PDF plugin on the browser? If a user had their browser configured to not display a pdf within the browser, would that break the functionality you are wanting to implement?

You may also want to consider a way of implementing Readspeakers Docreader into it for those agencies that use it.

joshua-graham-adelphi commented 5 years ago

Display inline with ckeditor.

yes they are aware, the functionality will not work if their web browser is not configured to say the pdf (they have download link too). In saying this, both firefox and chrome have built-in PDF readers OOTB :-) and they on;t be using IE if their organisation mandated it, which means they would likely mandate to have a pdf plug in for it.

They agency is retrospective fitting govcms 8 on to their existing druapl 8 site, so we know it works already as well.

brett-sargeant commented 5 years ago

cool. I tend to disable the viewing of PDFs in browser due to past bad experiences. There's also mobile browsers to consider, so as long as there's some way of getting to the pdf if the inline doesn't work it's good.

simesy commented 4 years ago

I'm not yes/no to this. Could be a great optional whitelisted module.

But in addition to your OP solution (which is impressive), it's possible to do iframes in ckeditor, it just requires adding the appropriate tags to the filter, and you have to edit the source.

Alternatively you could create a paragraph specifically for this, collect the inputs, and render the template output as an iframe.