Skip to content

Commit

Permalink
Include custom fields in Element::getIterator()
Browse files Browse the repository at this point in the history
Resolves #13009
  • Loading branch information
brandonkelly committed Mar 31, 2023
1 parent 7ff1e32 commit 952d371
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG-WIP.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

### Development
- The `|replace` Twig filter now supports passing in a hash with regular expression keys. ([#12956](https://github.com/craftcms/cms/issues/12956))
- Elements now include custom field values when being iterated over, and when being merged. ([#13009](https://github.com/craftcms/cms/issues/13009))

### Extensibility
- Added `craft\web\CpScreenResponseBehavior::$pageSidebar`, `pageSidebar()`, and `pageSidebarTemplate()`. ([#13019](https://github.com/craftcms/cms/pull/13019), [#12795](https://github.com/craftcms/cms/issues/12795))
Expand Down
22 changes: 22 additions & 0 deletions src/base/Element.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace craft\base;

use ArrayIterator;
use Craft;
use craft\behaviors\CustomFieldBehavior;
use craft\behaviors\DraftBehavior;
Expand Down Expand Up @@ -75,6 +76,7 @@
use DateTime;
use Illuminate\Support\Collection;
use Throwable;
use Traversable;
use Twig\Markup;
use UnitEnum;
use yii\base\ErrorHandler;
Expand Down Expand Up @@ -2283,6 +2285,26 @@ public function extraFields(): array
];
}

/**
* @inheritdoc
*/
public function getIterator(): Traversable
{
$attributes = $this->getAttributes();

// Include custom fields
if (static::hasContent() && ($fieldLayout = $this->getFieldLayout()) !== null) {
foreach ($fieldLayout->getCustomFieldElements() as $layoutElement) {
$field = $layoutElement->getField();
if (!isset($attributes[$field->handle])) {
$attributes[$field->handle] = $this->getFieldValue($field->handle);
}
}
}

return new ArrayIterator($attributes);
}

/**
* @inheritdoc
*/
Expand Down

0 comments on commit 952d371

Please sign in to comment.