Elgg  Version 5.1
FieldsService.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Forms;
4 
8 
15 
16  use Loggable;
17 
21  protected $fields = [];
22 
26  protected $events;
27 
31  protected $translator;
32 
40  $this->events = $events;
41  $this->translator = $translator;
42  }
43 
52  public function get(string $type, string $subtype): array {
53  if (isset($this->fields[$type][$subtype])) {
54  return $this->fields[$type][$subtype];
55  }
56 
57  $result = (array) $this->events->triggerResults('fields', "{$type}:{$subtype}", [
58  'type' => $type,
59  'subtype' => $subtype,
60  ], []);
61 
62  $fields = [];
63  // validate fields and generate labels
64  foreach ($result as $field) {
65  if (empty($field['name']) || empty($field['#type'])) {
66  $this->getLogger()->warning("Field config for '{$type}:{$subtype}' is missing 'name' or '#type' in field: " . print_r($field, true));
67  continue;
68  }
69 
70  if (!isset($field['#label'])) {
71  $label_key = "fields:{$type}:{$subtype}:{$field['name']}";
72  if ($this->translator->languageKeyExists($label_key)) {
73  $field['#label'] = $this->translator->translate($label_key);
74  }
75  }
76 
77  $fields[] = $field;
78  }
79 
80  $this->fields[$type][$subtype] = $fields;
81 
82  return $fields;
83  }
84 }
Events service.
$type
Definition: delete.php:22
trait Loggable
Enables adding a logger.
Definition: Loggable.php:14
Service for getting field definitions for type/subtype combinations.
if(elgg_extract('required', $vars)) if(elgg_extract('disabled', $vars)) $field
Definition: field.php:38
getLogger()
Returns logger.
Definition: Loggable.php:37
__construct(EventsService $events, Translator $translator)
Constructor.
$subtype
Definition: delete.php:23