Hi!

I was trying to add support in the "Full HTML" text filter by adding them to the "Block Formats" option under the CSS fieldset (see image 1) but it didn't work.

Image 1:
TinyMCE profile settings page for Full HTML. CSS fieldset near bottom of page is expanded to show the "Block Formats" text field.

When the block format selector is expanded in the wysiwyg, the new elements that were added do not display text. Upon clicking one of the label-less added elements, the editor does not update the current line and/or selected text's wrapping element to be what was chosen.

In the image below, you see the expanded select box and the blue stripe is where my mouse cursor was hovering when I took the screen shot.

Image 2:
TinyMCE "Block Format Selection" that displays block level elements that wrap the current selection in the text editor. Dropdown is displayed as expanded.

I confirmed that TinyMCE version 3.5.4.1 (which is the version I installed) is capable of adding new block level elements. The page where the settings that need to be used can be found is TinyMCE HTML5 Formats.

The image below shows how the dropdown should look when it is working properly.

Image 3:
TinyMCE "Block Format Selection" dropdown with correct values shown as expanded.

The code below includes the settings as copied from the page linked to above. Note the section starting with "// HTML5 formats".

tinyMCE.init({
        // General options
        mode : "textareas",
        theme : "advanced",
        plugins : "visualblocks,inlinepopups",

        // Theme options
        theme_advanced_buttons1 : "bold,italic,underline,strikethrough,styleselect,justifyleft,justifycenter,justifyright,justifyfull,|,visualblocks,code",
        theme_advanced_buttons2 : "",
        theme_advanced_buttons3 : "",
        theme_advanced_buttons4 : "",
        theme_advanced_toolbar_location : "top",
        theme_advanced_toolbar_align : "left",
        theme_advanced_statusbar_location : "bottom",
        theme_advanced_resizing : true,
        content_css : "/js/tinymce/examples/css/content.css",
        visualblocks_default_state: true,

        // Schema is HTML5 instead of default HTML4
        schema: "html5",

        // End container block element when pressing enter inside an empty block
        end_container_on_empty_block: true,

        // HTML5 formats
        style_formats : [
                {title : 'h1', block : 'h1'},
                {title : 'h2', block : 'h2'},
                {title : 'h3', block : 'h3'},
                {title : 'h4', block : 'h4'},
                {title : 'h5', block : 'h5'},
                {title : 'h6', block : 'h6'},
                {title : 'p', block : 'p'},
                {title : 'div', block : 'div'},
                {title : 'pre', block : 'pre'},
                {title : 'section', block : 'section', wrapper: true, merge_siblings: false},
                {title : 'article', block : 'article', wrapper: true, merge_siblings: false},
                {title : 'blockquote', block : 'blockquote', wrapper: true},
                {title : 'hgroup', block : 'hgroup', wrapper: true},
                {title : 'aside', block : 'aside', wrapper: true},
                {title : 'figure', block : 'figure', wrapper: true}
        ]
});

I tried looking into the code in tinymce.inc to try and find a clue as to where the problem might be but was unable to find anything...

I hope someone can help me out with this!

Thank you very much to the maintainers of this module!

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

TwoD’s picture

Category: bug » feature

The Block Formats list sets the theme_advanced_blockformats setting, not the style_formats setting, which was added later.

We don't have a GUI widget to control TinyMCE's style_formats setting yet, but you can control it using a small custom module until we do by implementing hook_wysiwyg_editor_settings_alter().

sites/default/modules/custom/custom.info:

name = Custom
description = My custom module for various tweaks.
core = 7.x

sites/default/modules/custom/custom.module:

/**
 * Implements hook_wysiwyg_editor_settings_alter().
 */
function custom_wysiwyg_editor_settings_alter(&$settings, $context) {
  if ($context['profile']->editor == 'tinymce') {
    $settings['style_formats'] = array(
      array('title' => 'h1', 'block' => 'h1'),
      array('title' => 'h2', 'block' => 'h2'),
      array('title' => 'h3', 'block' => 'h3'),
      array('title' => 'h4', 'block' => 'h4'),
      array('title' => 'h5', 'block' => 'h5'),
      array('title' => 'h6', 'block' => 'h6'),
      array('title' => 'p', 'block' => 'p'),
      array('title' => 'div', 'block' => 'div'),
      array('title' => 'pre', 'block' => 'pre'),
      array('title' => 'section', 'block' => 'section', 'wrapper' => TRUE, 'merge_siblings' => FALSE),
      array('title' => 'article', 'block' => 'article', 'wrapper' => TRUE, 'merge_siblings' => FALSE),
      array('title' => 'blockquote', 'block' => 'blockquote', 'wrapper' => TRUE),
      array('title' => 'hgroup', 'block' => 'hgroup', 'wrapper' => TRUE),
      array('title' => 'aside', 'block' => 'aside', 'wrapper' => TRUE),
      array('title' => 'figure', 'block' => 'figure', 'wrapper' => TRUE),
    );
  }
}
anandkp’s picture

That's awesome TwoD! Thank you very much for that!

Just a short while ago I came close to the same realisation... I'd found out that I was looking at the wrong dropdown. In the buttons settings, the box I thought would allow me to add wrappers is called "Block Formats" when, yes in fact, the dropdown I'd been hoping to update was called "Styles".

I had come up with a solution wherein the User could misappropriate the "Block Formats" text field to specify additional block wrapper elements... The code below demonstrates how I was using the elements defined there and adding them to the TinyMCE settings... this workaround was added directly to the tinymce-3.js file:

  Drupal.wysiwyg.editor.attach.tinymce = function(context, params, settings){

    // References:
    //    - http://www.tinymce.com/tryit/html5_formats.php
    //    - http://www.tinymce.com/wiki.php/Configuration:formats
    if(typeof settings.theme_advanced_blockformats !== 'undefined' && settings.theme_advanced_blockformats != false){

      // convenience assignment of current text filter settings object
      var master_settings_obj = settings;

      // split the User provided block element string into an array - string is set in the "Block formats"
      // option field located at: admin/config/content/wysiwyg/profile/TEXT FORMAT NAME/edit
      block_formats = master_settings_obj.theme_advanced_blockformats.replace(/\s+/g, '').split(',');

      // convenience assignment - an object to hold block format settings to add to settings for tinyMCE.init() below
      var block_format_elements_array = []; // For adding as master_settings_obj.style_formats

      // special settings - according to the help page mentioned below, some of the HTML5 elements require additional settings
      // I'm creating two arrays here with the names of those special elements so that I can check for them in the loop below
      var merge_siblings_array = ['section', 'article', 'blockquote', 'hgroup', 'aside', 'figure'];
      var wrapper_array = ['section', 'article'];

      // iterate over the formats and update blockFormatsSettings object as per instructions found here:
      // http://www.tinymce.com/tryit/html5_formats.php
      for(var index in block_formats){

        // convenience assignment
        var block_element = block_formats[index];

        // start adding necessary attributes
        var array_settings = {
          title : block_element,
          block : block_element
        };

        if($.inArray(block_element, merge_siblings_array) != -1){
          array_settings.merge_siblings = false;
        }

        if($.inArray(block_element, wrapper_array) != -1){
          array_settings.wrapper = true;
        }

        // update array
        block_format_elements_array.push(array_settings);
      }

      // Schema is HTML5 instead of default HTML4
      master_settings_obj.schema = "html5";

      // End container block element when pressing enter inside an empty block
      master_settings_obj.end_container_on_empty_block = true;

      // add the generated block_format_elements array to the master_settings_obj
      master_settings_obj.style_formats = block_format_elements_array;
    }
...

I'll move this stuff into a custom module as recommended by you.

I guess I should move this issue into feature requests... It would be really great if we could have a form area for TinyMCE were you could specify additional block elements that can act as wrappers.

Incidentally, there is one other setting worth noting in this issue and that's the "visualblocks" plugin. I added that in directly into the tinymce.inc file... I guess I'll be moving that out as well. The Visual Blocks plugin displays dashed-line wrappers that represent HTML block elements so that the editor can get a clearer idea about element nesting.

I'm new to Drupal and the community in general TwoD... could you tell me what you would the next step for this issue should be? I've noted in some posts/issues that the OP is asked to update the issue to be a feature request but I have no idea as to whether that's appropriate here and even if it is, I have no idea of how to do that...

Thank you very much for your help!

anandkp’s picture

FileSize
1.46 KB

For ease of access, I've created the custom module as recommended by TwoD above including the modification that allows access to the "Visual Blocks" plugin that was added from version 3.5.x.x for TinyMCE.

Hope that the custom module will prove useful to others who want to enable the use of HTML5 elements for their Users...

Cheers!

sun’s picture

Version: 7.x-2.1 » 7.x-2.x-dev

Err. That style_formats looks fairly similar to the one of CKEditor?
http://www.tinymce.com/wiki.php/Configuration:style_formats
http://docs.cksource.com/CKEditor_3.x/Developers_Guide/Configuration/Styles

We just extended the API to allow to configure this specifically for CKEditor Styles:
#1650416: Allow editor specific changes to be made to the profile settings form
#746524: No Font Styles for CKeditor

Now, the syntax/settings of TinyMCE appear to be almost exactly the same... ;)

alanom’s picture

Right, let's summarise.

If you want a) HTML5 block elements or b) Wrapper elements (e.g. a div with lots of paragraphs in it) in TinyMCE in Drupal, you can force them in as Styles that appear in the dropdown usually controlled by the CSS Styles box in the WYSIWYG module form.

You can do this two ways:

  1. The quick hard-coding approach. Download the module anandps provided above, modify to suit your case (e.g. it's set to only work in Full HTML mode, you might want Filtered HTML instead or as well), and hard-code in some styles. In its current form, it overrides anything entered into the CSS Styles form in the WYSIWYG module. To make something a wrapper, make sure it has wrapper => true, which is something specific to TinyMCE
  2. The long-term UI approach. WYSIWYG module only recently gained the ability to process things like CSS styles differently for different editors, and currently it's only set up for CKEditor which adds a similar feature. You could look at the issue where this was added, look at the patch that added the feature, and adapt it to work with TinyMCE (then share the patch here...). This might be part of the WYSIWYG module one day.

If you want to hard-code in a wrapper element that also has a class, it could look like this (note the slightly counter-intuitive 'classes' / string combo):

$settings['style_formats'][] = array(
        'title' => 'Wrapper (div)',
        'block' => 'div',
        'wrapper' => TRUE,
        'classes' => 'someclass, anotherclass'
      );

(tip for non-HTML5 wrappers: non-wrapper divs within a wrapper div are applied to the whole div. Non-wrapper <p>s within a wrapper div apply only to that paragraph)

The stuff about Visual Blocks is a separate bonus TinyMCE feature, adding a button that toggles dotted-line outlining of block elements. It's an alternative to adding visual cues to using a custom TinyMCE stylesheet defined in the WYSIWYG module CSS Settings form. Using one or the other is a good idea so your users can see the scope of wrapper elements intuitively.

alanom’s picture

Issue summary: View changes

Correcting name of file I looked in to find clues as to what the problem is.

suldan’s picture

Hello,
Is something new happening on this issue? I gave the module wysiwyg_customisations a try and it worked - thanks a lot. But I was wondering why we are mixing up block formats and styles? Wouldn't it be better to have the new html5 elements in the block formats dropdown?
Just replacing style_formats with block_formats in wysiwyg_customisations.module didn't do the trick.

When using this module I am missing my custom styles. Can't I have both?

Greetings
suldan

TwoD’s picture

Not much going on here, no. Not had time to dig into this.

If you're using TinyMCE 3, the block formats setting is called theme_advanced_blockformats, and it's also available for customization through hook_wysiwyg_editor_settings_alter(). Just note that since it's controllable via the editor profile GUI, it may already have a value (a string of comma-separated block tags).

If you're using Wysiwyg with the patch for TinyMCE 4, you can add the block_formats setting separately using the same hook.
Note that the block_formats setting takes a string, unlike the style_formats setting which takes an array.

igorski’s picture

FileSize
2.78 KB

The plugin from #3 has an issue which I have fixed now.

skylord’s picture

Status: Active » Needs review
FileSize
297 bytes

BTW, solved this issue with simple patch (attached) - after it block formats override works well for me in TinyMCE4.

  • TwoD committed 01825f3 on 6.x-2.x authored by skylord
    - #1665986 by skylord: Fixed the block_formats setting for TinyMCE.
    
  • TwoD committed 233670f on 7.x-2.x authored by skylord
    - #1665986 by skylord: Fixed the block_formats setting for TinyMCE.
    
TwoD’s picture

Status: Needs review » Fixed

Thanks for the patch @skylord, good catch!

I think I'm going to close this as "fixed" with this committed. The settings customization i mentioned in #7 still works and gives you total control of all editors which supports swapping out a block element's type like this.

The concerns mentioned in #6 about mixing "block formats" and "styles" I don't think I can really do much about without making things more confusing. As it currently is, the GUI widgets map to specific editor settings and tries to use their default values and functinality as established by the developers of each editor. If we go in and override these by say making an active choice to impose limitations on the profile GUI (say only allowing or defaulting to only using non-block element stuff in the styles area because that's already covered by another setting, etc), I think we'd end up having to make more and more tweaks until "everyone" is happy with the defaults, which is probably impossible. Sticking with what the editor does by default and not forcibly setting values on options that we haven't consciously touched in the GUI is a lot easier and would be much easier to stick with as the libraries evolve.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.