processwire / processwire-requests

ProcessWire feature requests.
40 stars 0 forks source link

Page Reference: Dynamic Parent Specification in Page Reference Field #507

Open klor opened 1 year ago

klor commented 1 year ago

Short description of the enhancement

Introduce dynamic Parent specification in the Page Reference field to allow project-specific value list creation and maintenance.

Optional: Steps that explain the enhancement

  1. Modify the Page Reference field settings to allow dynamic specification of Parent pages.
  2. Provide an option in the field settings to define conditions or rules for dynamic parent specification.
  3. Implement the functionality where the specified conditions or rules determine the set of available parent pages dynamically based on the project context.

Current vs. suggested behavior

Current Behavior: The Page Reference field allows users to create and maintain global value lists that are applicable across all projects in a system.

Suggested Behavior: The Page Reference field would offer an option to specify Parent pages dynamically, enabling users to create and maintain project-specific value lists. This would be particularly helpful for systems with multiple unique projects.

Why would the enhancement be useful to users?

Use cases:

Optional: Screenshots/Links that demonstrate the enhancement

image

Workaround using ready.php

Service info to those looking for a solution right now: Use this snippet:

$wire->addHookAfter('InputfieldPage::getSelectablePages', function($event) {
    // Field Project Milestone is used on Project Task Templates. Populate the Milestone field on the current Project Task with milestones for the current project.

    // field: project_milestone

        if($event->object->hasField == 'project_milestone') {
            $items = new PageArray;
            if( $event->arguments('page')->template == 'project' ) {
                $milestones = $event->arguments('page')->children('template=project_milestones,sort=sort');
                foreach($milestones as $milestone) {
                    if( $milestone->parent == $event->arguments('page') ) {
                        $items->add($milestone);
                    }
                }
            } else if( $event->arguments('page')->template == 'project-task' ) {
                $milestones = $event->arguments('page')->parent->children('template=project_milestones,sort=sort');
                foreach($milestones as $milestone) {
                    if( $milestone->parent == $event->arguments('page')->parent ) {
                        $items->add($milestone);
                    }
                }
            } else if( $event->arguments('page')->template == 'jtbd-step-need' ) {
                $milestones = $event->arguments('page')->parent->parent->children('template=project_milestones,sort=sort');
                foreach($milestones as $milestone) {
                    if( $milestone->parent == $event->arguments('page')->parent->parent ) {
                        $items->add($milestone);
                    }
                }
            }
            $event->return = $items;
        }
});