Jump to content
fruid

Exception: Unrecognized operator: &

Recommended Posts

I'm trying to assign a new parent to a set of pages according to a field of the page and the title of the new parent.

$some_pages = $pages('template=template_name');
foreach ($some_pages as $sp) {
    $new_parent = $sp->some_field->title; // selector field
    $sp->of(false);
    $sp->parent = $pages->get("name=$new_parent");
    $sp->save();
    $sp->of(true);
}

This code works fine except when it doesn't 😄 which is when there's a ÄÖÜ or whatnot in the title, then I get an Exception: Unrecognized operator: & Error.

I reckon that is because the browser receives e.g. Ü and turns it into Ü. But now it's all on the server and doesn't get converted? I guess the value in the mentioned field and of the title of the new page both store an &Uuml ? I always and still struggle with this kind of problems.

    $new_parent = htmlspecialchars($lc->club_region->title); // Ü

This doesn't work either, now the browser just shows me how it's stored, pointing out the very problem but no solution.

Thanks for enlightments. 

Share this post


Link to post
Share on other sites
21 hours ago, fruid said:

I'm trying to assign a new parent to a set of pages according to a field of the page and the title of the new parent.


$some_pages = $pages('template=template_name');
foreach ($some_pages as $sp) {
    $new_parent = $sp->some_field->title; // selector field
    $sp->of(false);
    $sp->parent = $pages->get("name=$new_parent");
    $sp->save();
    $sp->of(true);
}

 

First of all, what you're saying here seems a bit off: you mentioned using selector field, but judging from the value it looks like you might actually be using a Page reference field — is that correct, or is the field type really Selector?

Anyway, if it is a Page reference field, then $pages->get() is obsolete here: the stored value is already a Page. What you're doing here is taking that selected page, reading the value of the "title" field from it, and then asking ProcessWire to give you that same page again with $pages->get(), even though $sp->parent = $sp->some_field is all you really need to do. And even if you do have to get the parent with $pages-get(), you shouldn't match "title" to "name"; either match "name" to "name", or better yet use "id" (which is always unique across the system) 🙂

Also note that whenever you use $pages->get(), it will return any matching Page, including those that have been unpublished or trashed. In most cases you should use $pages->findOne() instead, or alternatively add some checks to confirm that this is indeed a valid (and non-trashed) page.

--

As for the error you're seeing, I think the biggest issue is that you're not sanitizing the selector value. Whenever you use a "dirty" value (including values stored in ProcessWire fields) in selectors, you should make sure it's valid. $sanitizer->selectorValue($new_parent) is the "generic" way to do this, but in this case you're using the value as a page name, so you should rather use $sanitizer->pageName($new_parent).

Note that if at this point $new_parent contains encoded entities, this could mean that you're using this code snippet in a context where output formatting is enabled. You can always get the "raw", unformatted value from a field with $page->getUnformatted(): $new_parent = $sp->some_field->getUnformatted('title').

--

Finally, depending on the use case you should know that this script could result in some errors or notices since you're not checking if $sp->some_field contains a value before you try to use it as an object. It's possible that you intentionally omitted some parts when posting the code snippet here, but if not, then you might want to add something like "if (!$sp->some_field) continue;" to the beginning of your foreach loop to skip empty values.

--

So, long story short: it's possible that the whole $pages->get() operation is unnecessary — but if that's not the case, then the issue is likely related to missing sanitization (and possibly output formatting) 🙂

  • Like 1

Share this post


Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...