Jump to content

ProcessWire Recipes


marcus

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
Link to comment
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 12
Link to comment
Share on other sites

  • 2 weeks later...

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
Link to comment
Share on other sites

  • 4 weeks later...

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
Link to comment
Share on other sites

  • 2 years later...

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
Link to comment
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.

Link to comment
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
Link to comment
Share on other sites

  • 5 years later...

As the project processwire-recipes.com went offline quite some time ago now,
I took the repository and deployed a browsable version of it over on:

https://processwire-recipes.pages.dev/

There is also a new repo:
https://github.com/webmanufaktur/processwire-recipes/

... on which this deployed version is based on.
Changes, fixes, updates can be submitted there.

Feel free to update links, fork the repo, submit changes for recipes layout or whatever.
It uses 11ty for rendering .md to .html and all the other magic.

I will add further details in regards to formatting recipes and submitting new content in the future.

 

Further notice:

@teppo: you link to the old domain on weekly.pw
Changes/Updates: incoming - right now this is WIP (work in progress)
Maintainers: feel free to let me know, I'll add you to that repo

 

Details:
the repo is on github: see above
hosting/deployment: on Cloudflare Pages via 11ty
updates: all updates against the master-branch will be deployed within 60 seconds after commit
changes: right now only via pull-request
maintainers: wanted - see above

  • Like 15
Link to comment
Share on other sites

Hi, original maintainer here.

Sorry about the unplanned phasing out of ProcessWire Recipes too long ago. Haven't done PW or any CMS work for that matter for a long time, so it's a shame that the PR project is dormant - in case it is still of value for people out there.

@wbmnfktr: Hit me up via DM if you're interested in taking over the Git Orga and/or domain

  • Like 2
Link to comment
Share on other sites

Another short update:
In the last few days @marcus and I talked and he made a member and owner of the "old" organization repo on Github so we can still use the old/former official repository - that's awesome!

Yet... the old domain is long gone.
I tried to contact the new owner but no chance so far.
Even through buyback-services and domainer/s.

Outlook:
I will update all recipes to the new markdown format, while keeping a copy of the legacy version in a separate branch/version.
I will update all guides on how to submit and update recipes in the next few days.

AND:
I bought the processwire.recipes domain - so I hope it was worth it. ?

MORE (ideas and changes and whatsoever):
Details will follow soon on processwire.recipes!


In other news:
Thanks @teppo for updating the link, I'll have to contact you again when the new domain and repo are online for the public. ?

  • Like 5
Link to comment
Share on other sites

@wbmnfktr Thank you so much for breathing life back into this project. Website already looks awesome.

I have an idea/suggestion: it would be great if all the code snippets could easily be integrated and used in our IDE. With the current way of how they are stored on GH I wouldn't how this could be achieved. There is a vscode extension that lets you use gists as code snippets. So if the actual code snippets were organized in gists it would be possible to use those from within the IDE. Maybe there is an automated way of storing all snippets as gists from within your build process?

  • Like 5
  • Thanks 1
Link to comment
Share on other sites

Thank you, @gebeer for the awesome feedback.

I love that idea! I wish we could create some sort of snippet collection (right now) from all of these recipes in some kind or another.

While it would be quite easy for me to export each recipe to another format of almost any kind, almost each and every recipe has to follow some or all rules of formatting. Right now... the formatting is super basic but we can work on it and even could create snippets for only those recipes that support the wanted formatting.

I could export those snippets, each and every time into another repository, when a recipe get's flagged as "isSnippet=true".
If we can find a workable solution and formatting for real snippets... I'm here to support it!

But first we should define a default/standard format for recipes and go from there.

I wish we could go through all of the recipes to make them some kind of uniform, working with ProcessWire 2+, and mark those which only work with ProcessWire 3, or whatever version of ProcessWire, just to find a solid foundation.

From there we could work out a snippet format and necessary fields, export them and even publish them to whereever we want.

Something I forgot... there are snippets, ready.php snippets, common hook snippets... and probably more... so we have to classify snippets and recipes right from the start in some kind. But sure... that's possible.

This will be fun!

I really love this idea.

  • Like 4
Link to comment
Share on other sites

59 minutes ago, gebeer said:

Thank you so much for breathing life back into this project.

I hope we all together can keep this project stay alive, and make it a super-awesome-meta-collection-of-goodness for ProcessWire.

There are so many awesome solutions and answers to common questions and challenges for whatever task buried deep into the forums.
From basic questions to full-blown Pro module solutions.

Maybe we all should be more sensitive to answers and solutions we get and at least should think about submitting those answers to the repository.

In my opinion this repo/project should have the goal to be the starting point for all ProcessWire Newcomers.

From easy tutorials to advanced setups. BUT... that's just my thought for the moment.

 

To make this whole project NOT my personal project, I will add more and more people from our community as maintainers/owners to this whole repo and will think about a solution to find a way to finance the domain without me. But those are future tasks. Just wanted to let you know. It's not about me, but the thought behind all this!

  • Like 5
Link to comment
Share on other sites

On 3/11/2023 at 10:58 AM, wbmnfktr said:

I wish we could create some sort of snippet collection (right now) from all of these recipes in some kind or another.

I found https://github.com/earldouglas/codedown which can be used to extract all code snippets programmatically from the markdown files.

With Jekyll, code from gists can be embedded into markdown files: https://gist.github.com/benbalter/5555251 Couldn't find anything for 11ty, though.

On 3/11/2023 at 10:58 AM, wbmnfktr said:

But first we should define a default/standard format for recipes and go from there.

I wish we could go through all of the recipes to make them some kind of uniform, working with ProcessWire 2+, and mark those which only work with ProcessWire 3, or whatever version of ProcessWire, just to find a solid foundation.

I browsed only through a few of the md files on https://github.com/webmanufaktur/processwire-recipes/tree/master/src/recipes and they looked quite uniform already. Haven't checked all of them, though. Are these all that exist?

Everybody should have moved to PW 3.x already. How would you identify 2.x snippets? Most of the code from 2.x still works in 3.x. So I don't think that this destinction is necessary.

As for the default standard. https://github.com/webmanufaktur/processwire-recipes/blob/master/src/pages/submit-recipe.md already is a good starting point. I don't think we need categories in addition to tags. Categorization can be done through tags in a flexible manner.

On 3/11/2023 at 10:58 AM, wbmnfktr said:

Something I forgot... there are snippets, ready.php snippets, common hook snippets... and probably more... so we have to classify snippets and recipes right from the start in some kind.

Couldn't we also use tags for that, like "init", "ready", "hook"? EDIT: I just did that and made a PR.

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

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