Use Entity Reference Prepopulate with Panelizer

Out of the box, Entity Reference Prepopulate doesn't work with forms rendered inside Panelizer modals. Using hook_entityreference_prepopulate_providers_info, we can extend Entity Reference Prepopulate to support Panelizer.

What is Entity Reference Prepopulate?

Entity Reference Prepopulate is a module that extends the Entity Reference module by allowing a site builder to prepopulate entity reference fields. For example, a site builder using Organic Groups could prepopulate the 'og_group_ref' field based on the AJAX path.

The first step is to define the provider:

<?php
/**
 * Implements hook_entityreference_prepopulate_providers_info.
 */

function MYMODULE_prepopulate_entityreference_prepopulate_providers_info() {
  return array(
   
'provider-name' => array(
     
'title' => t('Provider title'),
     
'description' => t('Provider description'),
     
'callback' => 'MYMODULE_entityreference_prepopulate_callback_function'
   
)
  );
}
?>

The next step is to decide what you want the callback function to do:

<?php
function MYMODULE_entityreference_prepopulate_callback_function {
 
// Do stuff here.
  // Return an entity id.
}
?>

Example Use Case

An example of where a site builder may use an entity reference field inside a Panelizer modal is when deploying Fieldable Panels Panes (FPP) in conjunction with Organic Groups Fieldable Panels Panes. The site builder desires that the FPP entity be automatically assigned to its group; however, the providers shipped Entity Reference Prepopulate are not able to discern the group context.

The first step is to define the provider:

<?php
/**
 * Implements hook_entityreference_prepopulate_providers_info.
 */

function MYMODULE_prepopulate_entityreference_prepopulate_providers_info() {
  return array(
   
'url-panel-edit' => array(
     
'title' => t('URL - Panel Edit Page'),
     
'description' => t('Prepopulate from URL within Panels edit pages'),
     
'callback' => 'MYMODULE_entityreference_prepopulate_callback_function'
   
)
  );
}
?>

The second step is to create a callback function to return a group id (GID). In this example, we're going to get the path from $_GET. Below is an example path:

panels/ajax/editor/add-pane/panelizer:node:2:default:4/center/fieldable_panels_pane/content

The callback function will first check to ensure we're looking at a panel modal being loaded over AJAX. Then it will check if Panelizer is being utilized. Next it will check if the entity is a group entity or a group content entity. Finally, it will use the relevant Organic Groups function to return the GID:

<?php
/**
 * Callback function for "url-panel-edit" provider.
 */

function MYMODULE_entityreference_prepopulate_callback_function($field, $instance) {
 
// Explode path into an array
 
$path = explode('/', $_GET['q']);
 
// If path starts with "panels", then evaluate further.
 
if ($path[0] == 'panels') {
   
// If path is from panelized entity, then return eid.
   
$panelizer = explode(':', $path[4]);
    if (
$panelizer[0] == 'panelizer') {
     
$eid = $panelizer[2];
     
$entity_type = $panelizer[1];
     
$return = array();
     
// If entity is a group type, then return its eid
     
if(og_is_group($entity_type, $eid)) {
       
$return[] = $eid;
      }
     
// If entity is not a group type, then look up and return group
      // In this application, we only allow a single group membership per entity
     
else {
       
$group = og_get_entity_groups($entity_type, $eid);
       
$return[] = array_shift($group[$entity_type]);
      }

      return
$return;
    }
  }
}
?>

The final step is to enable the new provider on the entity reference field of the FPP.

Add new comment