chg: [Component:CRUD] Only show used meta-template in view pages

pull/93/head
Sami Mokaddem 2022-03-01 15:21:56 +01:00
parent 5fa0280f15
commit 71cd1e307d
No known key found for this signature in database
GPG Key ID: 164C473F627A06FA
1 changed files with 13 additions and 7 deletions

View File

@ -204,7 +204,7 @@ class CRUDComponent extends Component
return false; return false;
} }
private function getMetaTemplates() private function getMetaTemplates(array $metaTemplateConditions=[])
{ {
$metaTemplates = []; $metaTemplates = [];
if (!$this->metaFieldsSupported()) { if (!$this->metaFieldsSupported()) {
@ -214,9 +214,10 @@ class CRUDComponent extends Component
$metaQuery = $this->MetaTemplates->find(); $metaQuery = $this->MetaTemplates->find();
$metaQuery $metaQuery
->order(['is_default' => 'DESC']) ->order(['is_default' => 'DESC'])
->where([ ->where(array_merge(
'scope' => $metaFieldsBehavior->getScope(), $metaTemplateConditions,
]) ['scope' => $metaFieldsBehavior->getScope(), ]
))
->contain('MetaTemplateFields') ->contain('MetaTemplateFields')
->formatResults(function (\Cake\Collection\CollectionInterface $metaTemplates) { // Set meta-template && meta-template-fields indexed by their ID ->formatResults(function (\Cake\Collection\CollectionInterface $metaTemplates) { // Set meta-template && meta-template-fields indexed by their ID
return $metaTemplates return $metaTemplates
@ -702,7 +703,12 @@ class CRUDComponent extends Component
if (empty($data)) { if (empty($data)) {
throw new NotFoundException(__('Invalid {0}.', $this->ObjectAlias)); throw new NotFoundException(__('Invalid {0}.', $this->ObjectAlias));
} }
$data = $this->attachMetaTemplatesIfNeeded($data); if ($this->metaFieldsSupported()) {
$usedMetaTemplateIDs = array_values(array_unique(Hash::extract($data['meta_fields'], '{n}.meta_template_id')));
$data = $this->attachMetaTemplatesIfNeeded($data, null, [
'id IN' => $usedMetaTemplateIDs
]);
}
if (isset($params['afterFind'])) { if (isset($params['afterFind'])) {
$data = $params['afterFind']($data); $data = $params['afterFind']($data);
} }
@ -715,7 +721,7 @@ class CRUDComponent extends Component
$this->Controller->set('entity', $data); $this->Controller->set('entity', $data);
} }
public function attachMetaTemplatesIfNeeded($data, array $metaTemplates = null) public function attachMetaTemplatesIfNeeded($data, array $metaTemplates = null, array $metaTemplateConditions=[])
{ {
if (!$this->metaFieldsSupported()) { if (!$this->metaFieldsSupported()) {
return $data; return $data;
@ -729,7 +735,7 @@ class CRUDComponent extends Component
return $tmpEntity; return $tmpEntity;
}, $metaTemplates); }, $metaTemplates);
} else { } else {
$metaTemplates = $this->getMetaTemplates()->toArray(); $metaTemplates = $this->getMetaTemplates($metaTemplateConditions)->toArray();
} }
$data = $this->attachMetaTemplates($data, $metaTemplates); $data = $this->attachMetaTemplates($data, $metaTemplates);
return $data; return $data;