marcus

ProcessWire Recipes

Recommended Posts

$last = $pagearray->pop();

echo $pagearray->implode( ', ', 'title', array('append' => ' & ') ) . $last->title;

 

array() is a php function, so "Boo!" :P

 
<?php
  $last = $pagearray->pop();
  echo $pagearray->implode( ', ', 'title', ['append' => ' & '] ) . $last->title;
:)
  • Like 3

Share this post


Link to post
Share on other sites

I see, this one needs a whole lot of cooking before it gets into the recipe book  :P

  • Like 2

Share this post


Link to post
Share on other sites

One more:

Loop pages in ProcessWire without building a $pageArray. This is useful for when a find() would return too many results to keep in memory.

$selector = "template=pages_template"; // as an example

while (1) {
    $p = wire('pages')->get("{$selector}, id>$id"); // get page with id bigger than previous
    if(!$id = $p->id) break; // assign current page's id to $id or break the loop if it doesn't exist

    // do stuff using $p as the current page 
    
    wire('pages')->uncacheAll();
};

This served me well when I had to modify thousands of pages in one go. Works great with bootstrapping from the terminal because it doesn't affect the viewing of the website.

  • Like 11

Share this post


Link to post
Share on other sites

One for https://processwire-recipes.com/

Output a Pagearray as: beer, rum, wine & whisky

(note the '&' and don't get drunk)

$count = count($pagearray);

foreach ($pagearray as $key => $p) {
	$divider = ($key + 1 === $count) ? ($count === 1 ? '' : ' & ') : ($key === 0 ? '' : ', ');
	$out .= $divider . "<a href='$p->url' class=''>$p->title</a>";
}

echo $out;

Back to this one :P

Not a pw recipe, sorry for that. This is only css and I thought it might be useful for this discussion.

.drink {
  display: inline;
}
.drink:nth-last-child(n+3):after {
  content: ", ";
}
.drink:nth-last-child(2):after {
  content: " & ";
}

http://codepen.io/diogo-ed/pen/OPwOJb

  • Like 7

Share this post


Link to post
Share on other sites

I use this kind of thing all the time for responsive menus ;)

Share this post


Link to post
Share on other sites

Can you give an example please how you use this script?

I use this kind of thing all the time for responsive menus

Share this post


Link to post
Share on other sites

I've found a very good one on debugging from adrian...;)

<?php //color code

/* Debug script for better output on vars and arrays in the browser js console
 * Thanks goes to https://processwire.com/talk/user/985-adrian/
 * Usage:
 *   debug('test');
 *   debug($variable);
 *   debug($array);
 */
function debug ($data) {
    echo "<script>\r\n//<![CDATA[\r\nif(!console){var console={log:function(){}}}";
    $output    =    explode("\n", print_r($data, true));
    foreach ($output as $line) {
        if (trim($line)) {
            $line    =    addslashes($line);
            echo "console.log(\"{$line}\");";
        }
    }
    echo "\r\n//]]>\r\n</script>";
}

best regards mr-fan

  • Like 3

Share this post


Link to post
Share on other sites

First contribution, "Yet another sorting of featured and non featured articles"

/* 
* Find the children by date, filter by featured (checkbox) and exclude this filtered item 
* from the final array so it won't appear repeated on pagination.
* If there's more than 1 featured item on Admin, sort them by date. 
* Useful when the user is lazy and marked several items as featured.
*/

$children = $page->children("sort=-published_at, limit=10");
$children->prepend($children->find("featured=1, limit=1, sort=-published_at"));
$featuredItem = $children->first();
$children = $children->not("$featuredItem");

If there's a better way, let me know! :)

 
Edited by Sergio
Improve comments

Share this post


Link to post
Share on other sites

Using multiple sorts wouldn't do? Of course there can be more than one featured item but imho that's a feature, why shouldn't be more than one? 

Share this post


Link to post
Share on other sites
4 hours ago, tpr said:

Using multiple sorts wouldn't do? Of course there can be more than one featured item but imho that's a feature, why shouldn't be more than one? 

Multiple sorts improve the memory usage or other measurable factor? I really don't know. If yes, great! :D

Regarding the feature limit, liked I said, users can be lazy and leave several itens marked as featured in the page tree. So that's an easy way to filter them when your front end page only accepts one featured item at a time.

Share this post


Link to post
Share on other sites
22 hours ago, Sergio said:

users can be lazy and leave several itens marked as featured in the page tree. So that's an easy way to filter them when your front end page only accepts one featured item at a time.

Here's one way to enforce only a single featured item while still enabling featured status to be set while editing an item (as opposed to using a Page Reference field on some other page to select a single featured item). In /site/ready.php...

$wire->addHookBefore('InputfieldCheckbox::render', function(HookEvent $event) {
    $inputfield = $event->object;
    if($this->process != 'ProcessPageEdit') return;
    $page = $this->process->getPage();
    // If this isn't the existing featured item...
    if($inputfield->hasField == 'featured' && $page->template == 'news_item' && !$page->featured) {
        // Add a note about the existing featured item
        $existing_featured_item = $this->pages->get("template=news_item, featured=1");
        $inputfield->description = "Only one item may have featured status. Featuring this item will un-feature '$existing_featured_item->title'.";
    }
});

$pages->addHookAfter('saveReady', function(HookEvent $event) {
    $page = $event->arguments(0);
    // If the "featured" checkbox is checked...
    if($page->template == 'news_item' && $page->featured) {
        // Remove the featured status of any other items
        $existing_featured_item = $this->pages->get("template=news_item, featured=1");
        if($existing_featured_item->id) $existing_featured_item->setAndSave('featured', 0);
    }
});

 

 

  • Like 4

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.