Skip to content

Commit

Permalink
fix: autoprepend causing wrong options field values
Browse files Browse the repository at this point in the history
  • Loading branch information
BernhardBaumrock committed Nov 9, 2023
1 parent 6efdf9d commit 27f9a04
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions AutoPrepend.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,28 @@
<?php namespace ProcessWire;
<?php

namespace ProcessWire;

/**
* This file is automatically added to the prependFile array.
* If you are using /site/templates/_init.php this file ensures that all defined
* variables and functions are available from every file that is rendered via
* RockFrontend.
*/
if($rockfrontend->autoprepended) return;
$vars = array_merge(get_defined_vars(), (array)$this->wire('all'));
foreach($vars as $k=>$v) $this->wire($k, $v);
if ($rockfrontend->autoprepended) return;

// this fixes the issue that $page->any_options_field->title
// shows the page title instead of the options' value title
// see https://processwire.com/talk/topic/29225-show-this-field-only-if-doesnt-seem-to-work/?do=findComment&comment=237096
$vars = get_defined_vars();
foreach ($page->getFields() as $field) {
if (!array_key_exists($field->name, $vars)) continue;
unset($vars[$field->name]);
}

// merge arrays and make them available as API variable
$vars = array_merge($vars, (array)$this->wire('all'));
foreach ($vars as $k => $v) {
$this->wire($k, $v);
}

$rockfrontend->autoprepended = true;

0 comments on commit 27f9a04

Please sign in to comment.