Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- function h5pmods_alter_semantics(&$semantics, $library_name) {
- /*
- * Check if the library name matches the content type that should be changed.
- * We don't have to do anything to Multiple Choice Quizzes, for example.
- */
- if ($library_name === 'H5P.PersonalityQuiz') {
- /*
- * We are now basically crawling through the semantics.json file hierarchy.
- * There are other ways to do this, but this should suffice for now.
- * $semantics contains all fields of the top hierarchy, and you're
- * looking for the one that is named 'personalities'. So, you can loop
- * over all the fields of $semantics and can look at each of them individually.
- * The one that you're currently looking at will be $semantic_field.
- * You take the next one, and the next one, ..., until you arrive at the one
- * that has the correct name.
- */
- foreach ($semantics as $semantic_field) {
- if ($semantic_field->name === 'personalities') {
- /*
- * $semantic_field is now the one we need, and we can dig deeper.
- * Looking at the semantics.json file, you can see that the next
- * hierarchy level is "inside" the key 'field', and then the next
- * one that you need is "inside" the key 'fields'. You could now even
- * go directly to the one field that you want, but maybe a fields
- * get's added, and that would screw up this script.
- * You don't really need the next step, but this might be easier to
- * grasp. You could now store all the information about a personality
- * (name, description and image) in another variable.
- */
- $personality_fields = $semantic_field->field->fields;
- /*
- * As before, you can loop over all of these until you find the one
- * that is of interest -- it has the name 'description'.
- */
- foreach ($personality_fields as $personality_field) {
- if ($personality_field->name === "description") {
- /*
- * Now you are at the right spot! $personality_field now
- * contains the field and all its properties that you want to
- * change.
- * You cannot magically create a button here, but you could, e.g.
- * change the widget type. In semantics.json it says:
- * "widget": "textarea"
- * That's how you tell the editor of H5P that it should create
- * a plain box where you can type into plain text. But H5P also
- * has these fancy editor boxes with lot's of buttons right?!
- * That's what's called the HTML-widget, and you can simply
- * change this by setting this ...
- */
- $personality_field->widget = 'html';
- /*
- * Now you have overridden semantics.
- */
- }
- }
- }
- }
- }
- }
- // You'll need something like the next line, too, if you're using WordPress
- //add_action('h5p_alter_library_semantics', 'h5pmods_alter_semantics', 10, 4);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement