Jump to content

Micro Contexts (aka field value contexts)


Recommended Posts

Micro Contexts (aka field value contexts)

Change any context-enabled setting in your page editor/template based on the value of a page reference field.

Why use this?

Say you have a template that is controlling some generic part of your website, like a section on the homepage, a widget, or an item in a media library. If you are using the various fields on this template in different ways depending on an option (page select) such as 'widget_type', 'media_type', etc, you can hot-switch the template context based on the field's value.

Why not use template, like just change the template?

Unfortunately, most of my clients would probably break down in tears if i tried to explain what a template is, or how to change it.

This also allows you to have better more relevant labels, descriptions and notes based on any page ref field value on your template, as well as be able to show/hide fields based on that value, or even move fields around to make some more prominent or visible.

How to use this

1.) Create a new template:

1a) Use the following name structure:

mc-[field_name]-[field_value]-[any_memo_here]

where [field_name] is the field that will control the micro context, and the [field_value] is the required value (page id) that the controller field must equal/have in order to trigger the virtual template context. The last part of the template name should be used for the page name, or some memo to make it easier to know from the templates list what field value will trigger the micro context, since the template name does require the ID value of the field, not the name.

1b) Make sure to specify the template you are contextualizing under "Duplicate fields used by another template" before clicking save!

1c) Edit your field settings for this template, for when your fieldname=value as specified in the template name (labels, descriptions, notes, visibility, field order etc.)

2.) Place this code in your ready.php file:

wire()->addHookAfter('ProcessPageEdit::loadPage', null, function (HookEvent $e) {
  $page = $e->return;
  $mcPlates = $this->wire('templates')->find("name^=mc-");
  if(!count($mcPlates)) return;

  foreach($mcPlates as $mcPlate) {
    $nameParts = explode('-', $mcPlate->name); // expected format: mc-[field_name]-[page-id]-[anything-you-want-here]
    $mc_field = $nameParts[1]; // the field controlling the micro context
    $mc_value = $nameParts[2]; // page id has to be the 3rd part of the template name

    $controllerField = $page->fields->get($mc_field);
    //if we find a matching controller field from the template name...
    if($controllerField) {
      $f_value = $page->get($controllerField->name);
      $proceed = false;
      if( ($f_value instanceof PageArray) || ($f_value instanceof Page) ) $proceed = true; // this only works with page reference fields.
      if(!$proceed) continue;

      // the page corresponding to the required value  (page id) specified in the mc template name, to change micro context
      $mc_page = wire('pages')->get((int) $mc_value); 
      // if the value specified in the mc template name matches the current value of the page reference field, change the 
      // fieldgroup context
      $proceed = false;
      if( $f_value instanceof PageArray && $f_value->has($mc_page) ) $proceed = true;
      if( $f_value instanceof Page && $f_value == $mc_page ) $proceed = true;
      if($proceed) {
        $tName = $mcPlate->name;
        $mc_template = wire('templates')->find("name=$tName")->first();
        if($mc_template) {
          $mc_fieldgroup = $mc_template->fieldgroup;
          $page->template->fieldgroup = $mc_fieldgroup;
        }
      }
    }
  }

});


Caveats

  1. This probably needs more work to be fully usable, and may need more bullet proofing in terms of checking for valid template name, etc.
  2. You can only have this work with 1 page reference field per template.
  3. This has been tested and is being used on at least 2 live sites, but caution should be used, and the functionality should be tested before using on a live site.

Screenshots:

1) a default widget with no field value context enabled fields

mc_default.thumb.jpg.0e0b91f8a44783a20b196b77ea74611c.jpg


2.) A field value context enabled edit page for the 'Widget Type', the template controlling this is now 'mc-widget_type-1054-text', but the template of the page stays as 'widget'; only the fieldgroup context is changed when the page is loaded to get the contextual settings:

mc_text.thumb.jpg.30430ee4c74e28a3bc4c4526351158a4.jpg

3.) the context for when the page reference field called 'widget_type' has the value of 1150, which is a video widget type (template name is 'mc-widget_type-1150-video').

mc_video.thumb.jpg.1a4f1529b5c9d2b4e4f20fa25ae4a245.jpg

 

Convenient Module edition coming soon!

 

  • Like 6
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...