Elgg  Version master
FieldsService.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Forms;
4 
8 
15 
16  use Loggable;
17 
18  protected array $fields = [];
19 
26  public function __construct(protected EventsService $events, protected Translator $translator) {
27  }
28 
37  public function get(string $type, string $subtype): array {
38  if (isset($this->fields[$type][$subtype])) {
39  return $this->fields[$type][$subtype];
40  }
41 
42  $result = (array) $this->events->triggerResults('fields', "{$type}:{$subtype}", [
43  'type' => $type,
44  'subtype' => $subtype,
45  ], []);
46 
47  $fields = [];
48  // validate fields and generate labels
49  foreach ($result as $field) {
50  if (empty($field['name']) || empty($field['#type'])) {
51  $this->getLogger()->warning("Field config for '{$type}:{$subtype}' is missing 'name' or '#type' in field: " . print_r($field, true));
52  continue;
53  }
54 
55  if (!isset($field['#label'])) {
56  $label_key = "fields:{$type}:{$subtype}:{$field['name']}";
57  if ($this->translator->languageKeyExists($label_key)) {
58  $field['#label'] = $this->translator->translate($label_key);
59  }
60  }
61 
62  $fields[] = $field;
63  }
64 
65  $this->fields[$type][$subtype] = $fields;
66 
67  return $fields;
68  }
69 }
__construct(protected EventsService $events, protected Translator $translator)
Constructor.
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