Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[4.x] Make tags with old __call methods macroable by using wildcard method instead. #9553

Merged
merged 6 commits into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/Events/CollectionTagFetchedEntries.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Statamic\Events;

use Statamic\Entries\EntryCollection;
use Statamic\Extensions\Pagination\LengthAwarePaginator;
use Statamic\Tags\Collection\Collection;

class CollectionTagFetchedEntries extends Event
{
public EntryCollection|LengthAwarePaginator $entries;
public Collection $tag;

public function __construct(EntryCollection|LengthAwarePaginator $entries, Collection $tag)
{
$this->entries = $entries;
$this->tag = $tag;
}
}
15 changes: 15 additions & 0 deletions src/Events/CollectionTagFetchingEntries.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Statamic\Events;

use Statamic\Tags\Collection\Collection;

class CollectionTagFetchingEntries extends Event
{
public Collection $tag;

public function __construct(Collection $tag)
{
$this->tag = $tag;
}
}
2 changes: 1 addition & 1 deletion src/Tags/Asset.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Asset extends Assets
*
* @return string
*/
public function __call($method, $arguments)
public function wildcard($method)
{
$value = Arr::get($this->context, $this->method);
$value = (array) $value;
Expand Down
2 changes: 1 addition & 1 deletion src/Tags/Assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class Assets extends Tags
*
* @return string
*/
public function __call($method, $arguments)
public function wildcard($method)
{
$value = Arr::get($this->context, $this->method);

Expand Down
15 changes: 10 additions & 5 deletions src/Tags/Collection/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Statamic\Tags\Collection;

use Statamic\Events\CollectionTagFetchedEntries;
use Statamic\Events\CollectionTagFetchingEntries;
use Statamic\Facades\Entry;
use Statamic\Tags\Concerns;
use Statamic\Tags\Tags;
Expand All @@ -15,13 +17,11 @@ class Collection extends Tags
/**
* {{ collection:* }} ... {{ /collection:* }}.
*/
public function __call($method, $args)
public function wildcard($method)
{
$this->params['from'] = $this->method;

return $this->output(
$this->entries()->get()
);
return $this->index();
}

/**
Expand All @@ -33,8 +33,11 @@ public function index()
return $this->context->value('collection');
}

$entries = $this->entries()->get();
CollectionTagFetchedEntries::dispatch($entries, $this);

return $this->output(
$this->entries()->get()
$entries
);
}

Expand Down Expand Up @@ -96,6 +99,8 @@ public function newer()

protected function entries()
{
CollectionTagFetchingEntries::dispatch($this);

return new Entries($this->params);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Tags/Glide.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class Glide extends Tags
* @param array $args
* @return string
*/
public function __call($method, $args)
public function wildcard($method)
{
$tag = explode(':', $this->tag, 2)[1];

Expand Down
2 changes: 1 addition & 1 deletion src/Tags/ParentTags.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class ParentTags extends Tags
* @param array $args
* @return mixed
*/
public function __call($method, $args)
public function wildcard($method)
{
$var_name = Stringy::removeLeft($this->tag, 'parent:');

Expand Down
2 changes: 1 addition & 1 deletion src/Tags/Scope.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class Scope extends Tags
{
public function __call($method, $args)
public function wildcard($method)
{
throw_unless($this->isPair, new \Exception('Scope tag must be a pair'));

Expand Down
2 changes: 1 addition & 1 deletion src/Tags/Section.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class Section extends Tags
{
public function __call($method, $args)
public function wildcard($method)
{
$name = explode(':', $this->tag)[1];

Expand Down
2 changes: 1 addition & 1 deletion src/Tags/Tags.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public function __call($method, $args)
if (static::hasMacro($method)) {
$macro = static::$macros[$method];

if ($macro instanceof Closure) {
if ($macro instanceof \Closure) {
$macro = $macro->bindTo($this, static::class);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Tags/Theme.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Theme extends Tags
* @param array $arguments Unused
* @return string
*/
public function __call($method, $arguments)
public function wildcard($method)
{
return $this->path($method);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Tags/Yields.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Yields extends Tags
{
protected static $aliases = ['yield'];

public function __call($method, $args)
public function wildcard($method)
{
$name = explode(':', $this->tag)[1];

Expand Down
Loading