getEntityName(), $this->getAction()); } catch (NotImplementedException $e) { } if (isset($actionClass) && method_exists($actionClass, 'fields')) { $values = $actionClass->fields(); } else { $values = $this->getRecords(); } $this->padResults($values); $result->exchangeArray($this->queryArray($values)); } /** * Ensure every result contains, at minimum, the array keys as defined in $this->fields. * * Attempt to set some sensible defaults for some fields. * * In most cases it's not necessary to override this function, even if your entity is really weird. * Instead just override $this->fields and thes function will respect that. * * @param array $values */ protected function padResults(&$values) { $fields = array_column($this->fields(), 'name'); foreach ($values as &$field) { $defaults = array_intersect_key([ 'title' => empty($field['name']) ? NULL : ucwords(str_replace('_', ' ', $field['name'])), 'entity' => $this->getEntityName(), 'required' => FALSE, 'options' => !empty($field['pseudoconstant']), 'data_type' => \CRM_Utils_Array::value('type', $field, 'String'), ], array_flip($fields)); $field += $defaults; if (!$this->loadOptions && isset($defaults['options'])) { $field['options'] = (bool) $field['options']; } $field += array_fill_keys($fields, NULL); } } /** * @return string */ public function getAction() { // For actions that build on top of other actions, return fields for the simpler action $sub = [ 'save' => 'create', 'replace' => 'create', ]; return $sub[$this->action] ?? $this->action; } public function fields() { return [ [ 'name' => 'name', 'data_type' => 'String', ], [ 'name' => 'title', 'data_type' => 'String', ], [ 'name' => 'description', 'data_type' => 'String', ], [ 'name' => 'default_value', 'data_type' => 'String', ], [ 'name' => 'required', 'data_type' => 'Boolean', ], [ 'name' => 'required_if', 'data_type' => 'String', ], [ 'name' => 'options', 'data_type' => 'Array', ], [ 'name' => 'data_type', 'data_type' => 'String', ], [ 'name' => 'input_type', 'data_type' => 'String', ], [ 'name' => 'input_attrs', 'data_type' => 'Array', ], [ 'name' => 'fk_entity', 'data_type' => 'String', ], [ 'name' => 'serialize', 'data_type' => 'Integer', ], [ 'name' => 'entity', 'data_type' => 'String', ], ]; } }