-
-
Save xnau/35093242336e9e57832b13c9059b6c3f to your computer and use it in GitHub Desktop.
| <?php | |
| /** | |
| * show grandchildren template | |
| * | |
| * this demonstrates a way to show the grandchildren of a parent type | |
| * | |
| */ | |
| // this is the name of the parent type this template is for | |
| $parent_type = 'teacher'; | |
| // this is the name of the child type that is the parent of the grandchildren | |
| $child_type = 'student'; | |
| // this is the name of the grandchild type to show | |
| $grandchild_fields = 'book'; | |
| // this checks to see if the current record has the parent type | |
| $is_parent_type = strpos( $this->participant_values[ \pdbmrdb\element\type_field::name ], '"' . $parent_type . '"' ); | |
| ?> | |
| <div class="wrap <?php esc_attr_e( $this->wrap_class ) ?>"> | |
| <?php | |
| if ( $this->record_found() ) : | |
| // output any validation errors | |
| $this->print_errors(); | |
| ?> | |
| <?php | |
| if ( ! $is_parent_type && array_key_exists('parent_pid', $_GET ) ) | |
| { | |
| // show the "back to parent" link | |
| $return_link = \Participants_Db::get_record_link( filter_input( INPUT_GET, 'parent_pid', FILTER_SANITIZE_FULL_SPECIAL_CHARS ) ); | |
| printf( '<p><a href="%s">Return to Parent Edit Page</a></p>', $return_link ); | |
| } | |
| ?> | |
| <?php | |
| // print the form header | |
| $this->print_form_head() | |
| ?> | |
| <?php while ( $this->have_groups() ) : $this->the_group(); ?> | |
| <fieldset class="field-group field-group-<?php esc_attr_e( $this->group->name ) ?> <?php echo $this->group->printing_title() ? 'group-with-title' : 'group-no-title' ?>"> | |
| <?php $this->group->print_title( '<legend>', '</legend>' ) ?> | |
| <?php $this->group->print_description() ?> | |
| <?php | |
| // step through the fields in the current group | |
| while ( $this->have_fields() ) : $this->the_field(); | |
| ?> | |
| <div class="form-group <?php $this->field->print_element_class() ?>"> | |
| <?php if ( $this->field->has_title() ) : ?> | |
| <label> | |
| <?php $this->field->print_label() ?> | |
| </label> | |
| <?php endif ?> | |
| <div class="input-group" > | |
| <?php if ( $is_parent_type && $this->field->name() === $child_type ) : // are we displaying the child type? | |
| // get the list of child types | |
| $student_list = \pdbmrdb\Record_Relations::type_relations( $child_type, $this->participant_id ); | |
| // this will display the grandchild records and format the display of the child records in the list | |
| $shortcode = '[pdb_single record_id=%d fields=' . $grandchild_fields . ' template="bare-field"]'; | |
| // display the list of child records using our special template | |
| foreach( $student_list as $record_id => $student_name ) | |
| { | |
| $student_record = \Participants_Db::get_participant( $record_id ); | |
| $edit_link = \Participants_Db::get_record_link( $student_record['private_id'] ) . '&parent_pid=' . $this->participant_values['private_id']; | |
| echo '<h4><a href="' . esc_attr( $edit_link ) . '" >' . esc_html($student_name) . '</a></h4>'; | |
| add_action( 'pdb-shortcode_set', function($shortcode) use($record_id){ | |
| $shortcode->shortcode_atts['record_id'] = $record_id; | |
| }); | |
| echo do_shortcode( sprintf( $shortcode, $record_id ) ); | |
| } | |
| else : // this is the normal field display | |
| $this->field->print_element_with_id(); | |
| endif ?> | |
| </div><!-- .input-group --> | |
| <?php if ( $this->field->has_help_text() && $this->field->name() !== $child_type ) : ?> | |
| <p class="help-block helptext"><?php $this->field->print_help_text() ?></p> | |
| <?php endif ?> | |
| </div><!-- .form-group --> | |
| <?php endwhile; // field loop ?> | |
| </fieldset><!-- .field-group-<?php esc_attr_e( $this->group->name ) ?> --> | |
| <?php endwhile; // group loop ?> | |
| <fieldset class="field-group field-group-submit"> | |
| <legend><?php $this->print_save_changes_label() ?></legend> | |
| <div class="form-group"> | |
| <button type="submit" class="btn btn-default" ><?php echo wp_kses_post( $this->shortcode_atts['submit_button'] ) ?></button> | |
| </div> | |
| </fieldset> | |
| <?php $this->print_form_close() ?> | |
| <?php else : ?> | |
| <?php $error_message = Participants_Db::plugin_setting( 'no_record_error_message', '' ); | |
| if ( !empty( $error_message ) ) : | |
| ?> | |
| <p class="alert alert-error"><?php echo wp_kses_post( $error_message ) ?></p> | |
| <?php endif ?> | |
| <?php endif ?> | |
| </div> |
The change I added on line 67 is needed because of a bug in the main plugin that doesn't allow a sub-record (a child record in this case) to be displayed if the parent record's PID is in the URL.
In this template there is a function that is used to generate a link:
\Participants_Db::get_record_link( $pid, $record_page )
If you want to have those links go to another page, you can include the name of that page as the second argument. You may want to do this if you need to use a separate page to display the "student" record. You would add the name of your student record page to that function on line 80 where the link to edit the student record is generated. Then, you would place the name of the page the parent record is edited on in the return link on line 36.
This way, when someone click on the student's name, they will go to the student record edit page to edit that record. At the top of that page, you can show a recturn link that will return the user to the parent record (teacher) edit page. If you want to use a different template for the student record edit page, you can copy the return link code to that template.
This template uses a utility template to show the grandchildren. The utility template must be included in your custom template for this template to work properly.
The utility template is found here: https://gist.github.com/xnau/b123857324ce37df9b0028bb1db19693