how to change a field widget

Hello!

I would like to customize the H5P.PersonalityQuiz content type so that one (or some, or all) of the fields of type "text" can be made to allow a few basic html tags (e.g., strong, em, a, etc.). I use Drupal and am trying use hook_h5p_semantics_alter to add the 'html' widget to every field of type "text," but clearly I'm not doing it right. 

Here's what my custom module (called mhl_h5p_mods) has right now:

function mhl_h5p_mods_h5p_semantics_alter(&$semantics, $machine_name, $major_version, $minor_version) {
  foreach ($semantics as $field) {
    // Go through list fields
    while ($field->type === 'list') {
      $field = $field->field;
    }

    // Go through group fields
    if ($field->type === 'group') {
      mhl_h5p_mods_h5p_semantics_alter($field->fields); 
      continue;
    }

    // Check to see if we have the correct type
    if ($field->type === 'text') {
      // Found a field. Add the html widget. [Does not work.]
      $field->widget = 'html';
    }
  }
}

I'm sure I'm missing something basic and appreciate any help people can give. Thanks in advance!

 

Content types: 
icc's picture

Your code looks fine but appears to be missing a & symbol in front of the $field variable in the foreach to make it a reference, like so:

foreach ($semantics as &$field) {

Perfect. Thank you!

I am using this code for my personality test but the buttons to add additional personalities and questions are gone now. Any suggestions? I really only need html for the description fields for the personalities. I am using drupal 7.

icc's picture

Could you share a screenshot of the issue and also include your code?

Have you checked the 'Recent log messages' in Drupal for anything that seems related to this after opening the editor?

Im using the same code as above. Without the custom module and code I have the ability to add other personality types, questions, and answers but with the code in those buttons go away for some reason. The html editor shows up but the buttons disapear.

icc's picture

It sounds like something must have gone wrong when you reused the code.

Is your module named the same as well? If not did you remember to update the naming of the function and then the usage of the function inside itself to be the same?

Did you remember to apply the fix I commented?

Are you getting any error messages in your browser's console or in the web server's error_log file?

I just tried the same code in my custom module and it works as expected. Did you make any modifications?

Hey sorry i was away for a couple days. Here is the code I have. The module is named h5p_custom.module.

function h5p_custom_h5p_semantics_alter(&$semantics, $machine_name, $major_version, $minor_version) {
  foreach ($semantics as &$field) {
    // Go through list fields
    while ($field->type === 'list') {
      $field = $field->field;
    }
 
    // Go through group fields
    if ($field->type === 'group') {
      h5p_custom_h5p_semantics_alter($field->fields); 
      continue;
    }
	
    // Check to see if we have the correct type
    if ($field->type === 'text') {
      // Found a field. Add the html widget.
      $field->widget = 'html';
    }
  }
}

 

fnoks's picture

Could you share a screenshot of the issue?