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

expanded export and datetime attrributes #16447

Merged
merged 4 commits into from
Jan 20, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions CHANGELOG-WIP.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
- Structure views are now available to element indexes on mobile browsers. ([#16190](https://github.com/craftcms/cms/discussions/16190))
- Datepickers now include a dropdown menu for selecting the year. ([#16376](https://github.com/craftcms/cms/pull/16376))
- Custom fields within element edit pages can now have action menus with “Copy value from site…”, “Edit field” and “Copy field handle” items. ([#16415](https://github.com/craftcms/cms/pull/16415), [#14056](https://github.com/craftcms/cms/pull/14056))
- Element exports now include date attribute values set to the system time zone. ([#16447](https://github.com/craftcms/cms/pull/16447))
- Heads-up displays now reposition themselves on window scroll.

### Accessibility
Expand Down
18 changes: 2 additions & 16 deletions src/base/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,10 @@
use craft\events\DefineFieldsEvent;
use craft\events\DefineRulesEvent;
use craft\helpers\App;
use craft\helpers\Component;
use craft\helpers\DateTimeHelper;
use craft\helpers\StringHelper;
use craft\helpers\Typecast;
use DateTime;
use ReflectionClass;
use ReflectionNamedType;
use ReflectionProperty;
use yii\validators\Validator;

/**
Expand Down Expand Up @@ -242,18 +239,7 @@ public function fields(): array
{
$fields = parent::fields();

$datetimeAttributes = [];
foreach ((new ReflectionClass($this))->getProperties(ReflectionProperty::IS_PUBLIC) as $property) {
if (!$property->isStatic()) {
$type = $property->getType();
if ($type instanceof ReflectionNamedType && $type->getName() === DateTime::class) {
$datetimeAttributes[] = $property->getName();
}
}
}

// Include datetimeAttributes() for now
$datetimeAttributes = array_unique(array_merge($datetimeAttributes, $this->datetimeAttributes()));
$datetimeAttributes = Component::datetimeAttributes($this);

// Have all DateTime attributes converted to ISO-8601 strings
foreach ($datetimeAttributes as $attribute) {
Expand Down
26 changes: 25 additions & 1 deletion src/elements/exporters/Expanded.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
use craft\base\ElementInterface;
use craft\elements\db\ElementQuery;
use craft\elements\db\ElementQueryInterface;
use craft\helpers\Component;
use craft\helpers\DateTimeHelper;
use craft\helpers\Db;

/**
Expand Down Expand Up @@ -63,7 +65,29 @@ public function export(ElementQueryInterface $query): mixed
unset($attributes[$field->handle]);
}
}
$elementArr = $element->toArray(array_keys($attributes));
// because of changes in https://github.com/craftcms/cms/commit/e662ee32d7a5c15dfaa911ae462155615ce7a320
// we need to split attributes to the date ones and all other;
// pass all other to toArray()
// and then return DateTimeHelper::toIso8601($date, false); for all the date ones
$datetimeAttributes = Component::datetimeAttributes($element);
$otherAttributes = array_diff(array_keys($attributes), $datetimeAttributes);

$elementArr = $element->toArray($otherAttributes);

foreach ($datetimeAttributes as $attribute) {
$date = $element->$attribute;
if ($date) {
$elementArr[$attribute] = DateTimeHelper::toIso8601($date);
} else {
$elementArr[$attribute] = $element->$attribute;
}
}

// sort the $elementArr so the keys are in the same order as the values in the $attributes table
uksort($elementArr, function($a, $b) use ($attributes) {
return $attributes[$a] <=> $attributes[$b];
});

if ($fieldLayout !== null) {
foreach ($fieldLayout->getCustomFields() as $field) {
$value = $element->getFieldValue($field->handle);
Expand Down
28 changes: 28 additions & 0 deletions src/helpers/Component.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@

use Craft;
use craft\base\ComponentInterface;
use craft\base\ElementInterface;
use craft\base\Model;
use craft\errors\MissingComponentException;
use DateTime;
use ReflectionClass;
use ReflectionNamedType;
use ReflectionProperty;
use yii\base\InvalidConfigException;

/**
Expand Down Expand Up @@ -176,4 +182,26 @@ public static function iconSvg(?string $icon, string $label): string

return Cp::iconSvg($icon, $label);
}

/**
* Return all DateTime attributes for given model.
*
* @param Model|ElementInterface $model
* @return array
*/
public static function datetimeAttributes(Model|ElementInterface $model): array
{
$datetimeAttributes = [];
foreach ((new ReflectionClass($model))->getProperties(ReflectionProperty::IS_PUBLIC) as $property) {
if (!$property->isStatic()) {
$type = $property->getType();
if ($type instanceof ReflectionNamedType && $type->getName() === DateTime::class) {
$datetimeAttributes[] = $property->getName();
}
}
}

// Include datetimeAttributes() for now
return array_unique(array_merge($datetimeAttributes, $model->datetimeAttributes()));
}
}