Use Entity Reference Prepopulate with Panelizer
Out of the box, Entity Reference Prepopulate (link is external) doesn't work with forms rendered inside Panelizer (link is external) 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 (link is external) module by allowing a site builder to prepopulate entity reference fields. For example, a site builder using Organic Groups (link is external) could prepopulate the 'og_group_ref' field based on the AJAX path.
The first step is to define the provider:
The next step is to decide what you want the callback function to do:
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 (link is external) (FPP) in conjunction with Organic Groups Fieldable Panels Panes (link is external). 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:
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:
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:
The final step is to enable the new provider on the entity reference field of the FPP.
Add new comment