Elgg  Version 6.2
FieldsService.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Forms;
4 
9 
16 
17  use Loggable;
18 
19  protected array $fields = [];
20 
28  public function __construct(protected EventsService $events, protected Translator $translator, protected EntityTable $entityTable) {
29  }
30 
39  public function get(string $type, string $subtype): array {
40  if (isset($this->fields[$type][$subtype])) {
41  return $this->fields[$type][$subtype];
42  }
43 
44  $entity_class = $this->entityTable->getEntityClass($type, $subtype);
45  $defaults = !empty($entity_class) ? $entity_class::getDefaultFields() : [];
46 
47  $result = (array) $this->events->triggerResults('fields', "{$type}:{$subtype}", [
48  'type' => $type,
49  'subtype' => $subtype,
50  ], $defaults);
51 
52  $fields = [];
53  // validate fields and generate labels
54  foreach ($result as $field) {
55  if (empty($field['name']) || empty($field['#type'])) {
56  $this->getLogger()->warning("Field config for '{$type}:{$subtype}' is missing 'name' or '#type' in field: " . print_r($field, true));
57  continue;
58  }
59 
60  if (!isset($field['#label'])) {
61  $label_key = "fields:{$type}:{$subtype}:{$field['name']}";
62  if ($this->translator->languageKeyExists($label_key)) {
63  $field['#label'] = $this->translator->translate($label_key);
64  }
65  }
66 
67  $fields[] = $field;
68  }
69 
70  $this->fields[$type][$subtype] = $fields;
71 
72  return $fields;
73  }
74 }
__construct(protected EventsService $events, protected Translator $translator, protected EntityTable $entityTable)
Constructor.
$defaults
Generic entity header upload helper.
Definition: header.php:6
Events service.
if(elgg_extract('input_type', $vars)) if(elgg_extract('required', $vars)) if(elgg_extract('disabled', $vars)) $field
Definition: field.php:42
$type
Definition: delete.php:21
trait Loggable
Enables adding a logger.
Definition: Loggable.php:14
Service for getting field definitions for type/subtype combinations.
getLogger()
Returns logger.
Definition: Loggable.php:37
$subtype
Definition: delete.php:22
Entity table database service.
Definition: EntityTable.php:24