Advertisement
Guest User

Untitled

a guest
Mar 1st, 2018
279
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.04 KB | None | 0 0
  1. <?php
  2.  
  3. function h5pmods_alter_semantics(&$semantics, $library_name) {
  4. /*
  5. * Check if the library name matches the content type that should be changed.
  6. * We don't have to do anything to Multiple Choice Quizzes, for example.
  7. */
  8. if ($library_name === 'H5P.PersonalityQuiz') {
  9. /*
  10. * We are now basically crawling through the semantics.json file hierarchy.
  11. * There are other ways to do this, but this should suffice for now.
  12. * $semantics contains all fields of the top hierarchy, and you're
  13. * looking for the one that is named 'personalities'. So, you can loop
  14. * over all the fields of $semantics and can look at each of them individually.
  15. * The one that you're currently looking at will be $semantic_field.
  16. * You take the next one, and the next one, ..., until you arrive at the one
  17. * that has the correct name.
  18. */
  19. foreach ($semantics as $semantic_field) {
  20. if ($semantic_field->name === 'personalities') {
  21. /*
  22. * $semantic_field is now the one we need, and we can dig deeper.
  23. * Looking at the semantics.json file, you can see that the next
  24. * hierarchy level is "inside" the key 'field', and then the next
  25. * one that you need is "inside" the key 'fields'. You could now even
  26. * go directly to the one field that you want, but maybe a fields
  27. * get's added, and that would screw up this script.
  28. * You don't really need the next step, but this might be easier to
  29. * grasp. You could now store all the information about a personality
  30. * (name, description and image) in another variable.
  31. */
  32. $personality_fields = $semantic_field->field->fields;
  33. /*
  34. * As before, you can loop over all of these until you find the one
  35. * that is of interest -- it has the name 'description'.
  36. */
  37. foreach ($personality_fields as $personality_field) {
  38. if ($personality_field->name === "description") {
  39. /*
  40. * Now you are at the right spot! $personality_field now
  41. * contains the field and all its properties that you want to
  42. * change.
  43. * You cannot magically create a button here, but you could, e.g.
  44. * change the widget type. In semantics.json it says:
  45. * "widget": "textarea"
  46. * That's how you tell the editor of H5P that it should create
  47. * a plain box where you can type into plain text. But H5P also
  48. * has these fancy editor boxes with lot's of buttons right?!
  49. * That's what's called the HTML-widget, and you can simply
  50. * change this by setting this ...
  51. */
  52. $personality_field->widget = 'html';
  53. /*
  54. * Now you have overridden semantics.
  55. */
  56. }
  57. }
  58.  
  59. }
  60. }
  61. }
  62. }
  63.  
  64. // You'll need something like the next line, too, if you're using WordPress
  65. //add_action('h5p_alter_library_semantics', 'h5pmods_alter_semantics', 10, 4);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement