Skip to content

Commit 7297624

Browse files
committed
fix(entityconfig): enhance lazy creation of entity config
1 parent 1e7fb3a commit 7297624

File tree

1 file changed

+24
-11
lines changed

1 file changed

+24
-11
lines changed

inc/entityconfig.class.php

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,9 @@ public function prepareInputForAdd($input) {
148148
}
149149

150150
public function prepareInputForUpdate($input) {
151+
// Disallow changing the linked entity
152+
unset($input['entities_id']);
153+
151154
$input['header'] = $input['header'] ?? '';
152155

153156
$config = Toolbox::getHtmLawedSafeConfig();
@@ -156,22 +159,31 @@ public function prepareInputForUpdate($input) {
156159
return $input;
157160
}
158161

162+
protected static function createDefaultsForEntity($entityId): self {
163+
$entityConfig = new self();
164+
if ($entityConfig->getFromDbByCrit(['entities_id' => $entityId])) {
165+
return $entityConfig;
166+
}
167+
168+
$entityConfig->add([
169+
'entities_id' => $entityId,
170+
'replace_helpdesk' => self::CONFIG_PARENT,
171+
'is_kb_separated' => self::CONFIG_PARENT,
172+
'is_search_visible' => self::CONFIG_PARENT,
173+
'is_header_visible' => self::CONFIG_PARENT,
174+
'sort_order' => self::CONFIG_PARENT,
175+
]);
176+
177+
return $entityConfig;
178+
}
179+
159180
public function showFormForEntity(Entity $entity) {
160-
$ID = $entity->getField('id');
181+
$ID = $entity->getID();
161182
if (!$entity->can($ID, READ)) {
162183
return false;
163184
}
164185

165-
if (!$this->getFromDbByCrit(['entities_id' => $ID])) {
166-
$this->add([
167-
'entities_id' => $ID,
168-
'replace_helpdesk' => self::CONFIG_PARENT,
169-
'is_kb_separated' => self::CONFIG_PARENT,
170-
'is_search_visible' => self::CONFIG_PARENT,
171-
'is_header_visible' => self::CONFIG_PARENT,
172-
'sort_order' => self::CONFIG_PARENT,
173-
]);
174-
}
186+
$this->getFromDB(self::createDefaultsForEntity($ID)->getID());
175187

176188
$canedit = Entity::canUpdate() && $entity->canUpdateItem();
177189
echo "<div class='spaced'>";
@@ -389,6 +401,7 @@ static function getUsedConfig($fieldref, $entities_id, $fieldval = '', $default_
389401
// Search in entity data of the current entity
390402
if ($entity->getFromDB($entities_id)) {
391403
// Value is defined : use it
404+
self::createDefaultsForEntity($entities_id);
392405
if ($entityConfig->getFromDBByCrit(['entities_id' => $entities_id])) {
393406
if (is_numeric($default_value)
394407
&& ($entityConfig->fields[$fieldref] != self::CONFIG_PARENT)) {

0 commit comments

Comments
 (0)