Jump to content

'$this' in text formatter module while using urlSegment


Recommended Posts

Posted

Heyhey,

run into this issue:

if I access an URL read/x where x is a url segment. Then, in read.php (template for read) i find the page x and display a text field it has together with some text formatter I wrote that manipulates the text field.

The problem is that inside the text formatter code $this->page points to read, not x – so my filter isn't working, because I'm trying to fetch data from other fields in x (I'm writing a filter for footnotes).

Am I doing it wrong or is it a bug? (I'm probably doing it wrong).

thanks!

Posted

Have you read this: http://wiki.processwire.com/index.php/URL_Segments

From what I think you want to do, you need to refer to x as:

$input->urlSegment1

But then if x is a url segment, then I am not sure how it can have other fields, which is what you describe. Perhaps x is the name of another page somewhere in the PW tree? If so, then you could do:

$pages->get("name={$input->urlSegment1}")->field_you_are_after_from_page_x

But maybe I am completely not understanding what you want here!

Posted (edited)

Thanks but, from inside the text formatter module code there is no "$page", only "$this->page", my problem is that $this->page refers to the wrong page. Not the page where the text field and textformatter resides.

So, I can't do "$page->urlSegment", I don't think I can get the current urlSegment from within a text formatter, in any case it doesn't feel like a good idea.

i found an easy workaround to simply inject the fields I need before I use the text field (that calls the text formatters), no big problems - but it's a little bit ugly

like

$page->footnotes = $pages->get($page->urlSegment(1))->footnotes //hack in read.php

then in my TextFormatterFootnotes.php i can do 

$this->page->footnotes

j

Edited by joe_g
Posted

Ok, sorry for my confusion.

$this->page is what is used in modules in place of $page - it's effectively the same thing. $page and $this->page will always reference the current page.

Remember that URL Segments are not pages. They are just a way to pass variables to a page so you can control the content that is being output. 

I guess you're trying to apply the text formatter to the content that is being pulled in from page x, but I think I would need to see your template code to get a better idea of what is going on. Maybe someone else can visualize it already?

Posted

There's no $page->urlSegment only $input->urlSegment.

You problem is not a bug. You're using a context in a Textformatter, but it's of course in the context of the current page as I understand what you saying. So you would need to do something differently.

There's many ways and solutions I guess, but without seeing your Textformatter it's hard. One would be to set the $page to the one you render the field from and set it back to the page before. etc.

Posted

thanks, switching context would work. I'll try that.

I thought the context would be the calling page, not the current page. But I guess it makes sense how things are.

So, if I do

$somepage = $page->get('/somepage');
echo $somepage->body; // body has MyTextFormatter filter 

in MyTextFormatter.php, $this->page is equal to $page, not $somepage. This is what confused me.

J

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...