Skip to content

Commit

Permalink
Add setting to exclude field classes from response
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelreichor committed Dec 30, 2024
1 parent 7b73f09 commit f42fe9e
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 7 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,5 @@

- Add support for search query.
- Add `EVENT_REGISTER_ELEMENT_TYPES` for defining custom element types.
- Add settings for cache duration.
- Add settings for cache duration and exclude Field Classes.
- Change file and class name of `FieldTransformerEvent` to `RegisterFieldTransformersEvent`.
4 changes: 4 additions & 0 deletions src/config/craft-query-api.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
<?php
/*
return [
// Defines the cache duration. Defaults to the cache duration defined in your general.php.
'cacheDuration' => 69420,
// Define field classes that should be excluded from the response.
'excludeFieldClasses' => ['nystudio107\seomatic\fields\SeoSettings']
];*/
8 changes: 8 additions & 0 deletions src/models/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,13 @@

class Settings extends Model
{
/**
* Defines the cache duration. Defaults to the cache duration defined in your general.php.
*/
public ?int $cacheDuration = null;

/**
* Define field classes that should be excluded from the response.
*/
public array $excludeFieldClasses = [];
}
21 changes: 15 additions & 6 deletions src/transformers/BaseTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use craft\errors\ImageTransformException;
use craft\errors\InvalidFieldException;
use samuelreichoer\queryapi\events\RegisterFieldTransformersEvent;
use samuelreichoer\queryapi\QueryApi;
use yii\base\InvalidConfigException;

abstract class BaseTransformer extends Component
Expand All @@ -16,11 +17,17 @@ abstract class BaseTransformer extends Component
public const EVENT_REGISTER_FIELD_TRANSFORMERS = 'registerTransformers';
private $customTransformers;

private array $excludeFieldClasses = ['nystudio107\seomatic\fields\SeoSettings'];

public function __construct(ElementInterface $element)
{
parent::__construct();
$this->element = $element;
$this->registerCustomTransformers();

if(QueryApi::getInstance()->getSettings()->excludeFieldClasses){

Check failure on line 28 in src/transformers/BaseTransformer.php

View workflow job for this annotation

GitHub Actions / PHPStan

Access to an undefined property craft\base\Model::$excludeFieldClasses.
$this->excludeFieldClasses = array_merge( $this->excludeFieldClasses, QueryApi::getInstance()->getSettings()->excludeFieldClasses);
}
}

/**
Expand Down Expand Up @@ -56,9 +63,6 @@ protected function getTransformedFields(array $predefinedFields = []): array
$nativeFields = $fieldLayout ? $fieldLayout->getAvailableNativeFields() : [];
$transformedFields = [];

$filteredOutClasses = [
'nystudio107\seomatic\fields\SeoSettings',
];

// Transform native fields if they are in predefinedFields (if specified)
foreach ($nativeFields as $nativeField) {
Expand All @@ -76,7 +80,7 @@ protected function getTransformedFields(array $predefinedFields = []): array
$fieldValue = $this->element->$fieldHandle;
$fieldClass = get_class($nativeField);

if (in_array($fieldClass, $filteredOutClasses, true)) {
if (in_array($fieldClass, $this->excludeFieldClasses, true)) {
continue;
}

Expand All @@ -94,8 +98,7 @@ protected function getTransformedFields(array $predefinedFields = []): array

$fieldValue = $this->element->getFieldValue($fieldHandle);
$fieldClass = get_class($field);

if (in_array($fieldClass, $filteredOutClasses, true)) {
if (in_array($fieldClass, $this->excludeFieldClasses, true)) {
continue;
}

Expand Down Expand Up @@ -212,6 +215,12 @@ protected function transformMatrixField(array $matrixFields): array
foreach ($block->getFieldValues() as $fieldHandle => $fieldValue) {
$field = $block->getFieldLayout()->getFieldByHandle($fieldHandle);
$fieldClass = get_class($field);

// Exclude fields in matrix blocks
if (in_array($fieldClass, $this->excludeFieldClasses, true)) {
continue;
}

$blockData[$fieldHandle] = $this->transformCustomField($fieldValue, $fieldClass);
}

Expand Down

0 comments on commit f42fe9e

Please sign in to comment.