Jump to content

Using multi-instance in FieldtypePage


Ferdi Derksen
 Share

Recommended Posts

Hi guys,

I was wondering if there is an option to load another site in the FieldtypePage by using the custom PHP code to find selectable pages.

// placed as custom PHP code to find selectable pages
return $content->pages->get("/")->children();

When it needs hooking into methods as "InputfieldPage::___getSelectablePages" or "InputfieldPage::___findPagesCode", I'd like to know how.

By just placing the following code somewhere in the methods it won't work, but maybe someone else knows how?

$content = new ProcessWire('/mypath/to/content/site/', '/content/'); // placed in subdirectory

 

Maybe it isn't even possible, but then also, I'd like to know :).

Link to comment
Share on other sites

I guess you're wrong this time ;)

With a hook I managed to accomplish my question. My finished code looks like this:

public function init() {
  $this->pages->addHookBefore('InputfieldPage::findPagesCode', $this, 'findPortalPagesCode'); 
}

public function findPortalPagesCode($event) {
    if (!$event->object->findPagesCode) return;
    $event->replace = true;

 // @TODO check for value in findPagesCode before loading $content so it won't load every time
    $content = new ProcessWire('/mypath/to/content/site/', '/content/');

    if($event->object) {}

    $pages = $this->wire('pages'); // so that it is locally scoped to the eval
    if(empty($event->object->findPagesCode)) $event->return = $pages->newPageArray();
    $event->return = eval($event->object->findPagesCode);
}

At the fieldsettings I placed my custom PHP code as selector like this:

return $content->find("template=news");

 

Link to comment
Share on other sites

True, but I don't want to mix, these pages are only be available in certain cases. Because these fields are only set for a specific template we can get content from another PW installation and won't mix them up with the original installation.

Have to say I should have mentioned that indeed.

For this purpose only I also set a hook on InputfieldPage::getSelectablePages to be certain we could get the page when the current page has the same ID as in the other installation.

// in short I removed this bit
if($children && $children->has($page)) {
  $children->remove($page); // don't allow page being edited to be selected
}

 

Link to comment
Share on other sites

And just to make that one clear, you shouldn't even need to hook anything. This should just work fine by putting this into the custom php code for the field:

$contentWire = new ProcessWire('/mypath/to/content/site/', '/content/');
return $contentWire->wire('pages')->find('…');

If you want to maintain the reference throughout the request (not creating the instance multiple times) I'd suggest doing this in the init/ready.php:

$contentWire = new ProcessWire('/mypath/to/content/site/', '/content/');
$this->wire('content', $contentWire, true);

This does register a new processwire api variable $content for the second instance, available in custom php code like this:

return $this->wire('content')->wire('pages')->find('…');

 

Link to comment
Share on other sites

That's not correct. $page and $pages are the only api variables preregistered as local variables. Other processwire api variables are available via the access method in classes: $this->wire('api-name'). And you can always create as many own variables as you like in there. Nobody needs you to use the local variables $pages/$page for the code. They're just there for convenience.

  • Like 2
Link to comment
Share on other sites

Tnx for the input, but unfortunately I didn't get that to work, it results in a fatal error;

Fatal error: Call to a member function wire() on a non-object in /mypath/htdocs/wire/modules/Inputfield/InputfieldPage/InputfieldPage.module(283) : eval()'d code on line 1

Placed in my ready.php file:

$contentWire = new ProcessWire('/mypath/to/content/site', '/content/');
wire('content', $contentWire, true); // using $this->wire.... didn't work

Placed in the field PHP code:

return $this->wire('content')->wire('pages')->find("parent=/");

 

I hope you have any thoughts left? Thanks in advance!

Link to comment
Share on other sites

  • 7 months later...
On 12/7/2016 at 9:24 AM, Ferdi Derksen said:

Tnx for the input, but unfortunately I didn't get that to work, it results in a fatal error;

Fatal error: Call to a member function wire() on a non-object in /mypath/htdocs/wire/modules/Inputfield/InputfieldPage/InputfieldPage.module(283) : eval()'d code on line 1

Placed in my ready.php file:


$contentWire = new ProcessWire('/mypath/to/content/site', '/content/');
wire('content', $contentWire, true); // using $this->wire.... didn't work

Placed in the field PHP code:


return $this->wire('content')->wire('pages')->find("parent=/");

 

I hope you have any thoughts left? Thanks in advance!

 

I know this is old, but I think your error might be because you're trying to attach $content to the current instance of ProcessWire.

I think it might work if you did this:

return $content->wire('pages')->find("parent=/");

 

Since $content is a new instance of ProcessWire, you should be able to do things like this:

$contentModules = $content->wire('modules');
$contentForms = $contentModules->get('FormBuilder');

 

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...