Helper function avoids verbose coding pattern.
// Fetch default values for fields that are writeable
$condition = [['type', '=', 'Field'], ['readonly', 'IS EMPTY'], ['default_value', '!=', 'now']];
// Exclude "weight" as that auto-adjusts
- if (in_array('SortableEntity', CoreUtil::getInfoItem($item['entity_type'], 'type'), TRUE)) {
+ if (CoreUtil::isType($item['entity_type'], 'SortableEntity')) {
$weightCol = CoreUtil::getInfoItem($item['entity_type'], 'order_by');
$condition[] = ['name', '!=', $weightCol];
}
$idField = CoreUtil::getIdFieldName($item['entity_type']);
$params['values'][$idField] = $item['entity_id'];
// Exclude "weight" as that auto-adjusts
- if (in_array('SortableEntity', CoreUtil::getInfoItem($item['entity_type'], 'type'), TRUE)) {
+ if (CoreUtil::isType($item['entity_type'], 'SortableEntity')) {
$weightCol = CoreUtil::getInfoItem($item['entity_type'], 'order_by');
unset($params['values'][$weightCol]);
}
protected function writeObjects($items) {
$updateWeights = FALSE;
// Adjust weights for sortable entities
- if (in_array('SortableEntity', CoreUtil::getInfoItem($this->getEntityName(), 'type'))) {
+ if (CoreUtil::isType($this->getEntityName(), 'SortableEntity')) {
$weightField = CoreUtil::getInfoItem($this->getEntityName(), 'order_by');
// Only take action if updating a single record, or if no weights are specified in any record
// This avoids messing up a bulk update with multiple recalculations
*/
private function getQuery() {
if (!isset($this->_selectQuery) && !empty($this->savedSearch['api_entity'])) {
- if (!in_array('DAOEntity', CoreUtil::getInfoItem($this->savedSearch['api_entity'], 'type'), TRUE)) {
+ if (!CoreUtil::isType($this->savedSearch['api_entity'], 'DAOEntity')) {
return $this->_selectQuery = FALSE;
}
$api = Request::create($this->savedSearch['api_entity'], 'get', $this->savedSearch['api_params']);
return $provider->getEntities()[$entityName][$keyToReturn] ?? NULL;
}
+ /**
+ * Check if entity is of given type.
+ *
+ * @param string $entityName
+ * e.g. 'Contact'
+ * @param string $entityType
+ * e.g. 'SortableEntity'
+ * @return bool
+ */
+ public static function isType(string $entityName, string $entityType): bool {
+ $entityTypes = (array) self::getInfoItem($entityName, 'type');
+ return in_array($entityType, $entityTypes, TRUE);
+ }
+
/**
* Get name of unique identifier, typically "id"
* @param string $entityName
}
// Add primary key field if actions are enabled
// (only needed for non-dao entities, as Api4SelectQuery will auto-add the id)
- if (!in_array('DAOEntity', CoreUtil::getInfoItem($this->savedSearch['api_entity'], 'type')) &&
+ if (!CoreUtil::isType($this->savedSearch['api_entity'], 'DAOEntity') &&
(!empty($this->display['settings']['actions']) || !empty($this->display['settings']['draggable']))
) {
$this->addSelectExpression(CoreUtil::getIdFieldName($this->savedSearch['api_entity']));
$deleteResult = $deleteAction->execute();
});
- if (in_array('DAOEntity', CoreUtil::getInfoItem($entityName, 'type'))) {
+ if (CoreUtil::isType($entityName, 'DAOEntity')) {
// We should have emitted an event.
$hookEntity = ($entityName === 'Contact') ? 'Individual' : $entityName;/* ooph */
$this->assertContains("pre.{$hookEntity}.delete", $log, "$entityName should emit hook_civicrm_pre() for deletions");
* @return bool
*/
protected function isReadOnly($entityName) {
- return in_array('ReadOnlyEntity', CoreUtil::getInfoItem($entityName, 'type'), TRUE);
+ return CoreUtil::isType($entityName, 'ReadOnlyEntity');
}
/**
$entities = Entity::get(FALSE)->addSelect('name')->execute()->column('name');
$actions = ['get', 'save', 'create', 'update', 'delete', 'replace', 'revert', 'export', 'autocomplete', 'getFields', 'getActions', 'checkAccess'];
$properties = Entity::getFields(FALSE)->addOrderBy('name')->execute()->column('name');
+ $entityTypes = Entity::getFields(FALSE)->addWhere('name', '=', 'type')->setLoadOptions(TRUE)->execute()->first()['options'] ?? [];
$builder = new PhpStormMetadata('api4', __CLASS__);
$builder->registerArgumentsSet('api4Entities', ...$entities);
$builder->registerArgumentsSet('api4Actions', ...$actions);
$builder->registerArgumentsSet('api4Properties', ...$properties);
+ $builder->registerArgumentsSet('api4EntityTypes', ...array_values($entityTypes));
// Define arguments for core functions
$builder->addExpectedArguments('\civicrm_api4()', 0, 'api4Entities');
$builder->addExpectedArguments('\Civi\Api4\Utils\CoreUtil::getTableName()', 0, 'api4Entities');
$builder->addExpectedArguments('\Civi\Api4\Utils\CoreUtil::getCustomGroupExtends()', 0, 'api4Entities');
$builder->addExpectedArguments('\Civi\Api4\Utils\CoreUtil::getRefCount()', 0, 'api4Entities');
+ $builder->addExpectedArguments('\Civi\Api4\Utils\CoreUtil::isType()', 0, 'api4Entities');
+ $builder->addExpectedArguments('\Civi\Api4\Utils\CoreUtil::isType()', 1, 'api4EntityTypes');
$builder->addExpectedArguments('\Civi\Api4\Utils\CoreUtil::checkAccessDelegated()', 0, 'api4Entities');
$builder->addExpectedArguments('\Civi\Api4\Utils\CoreUtil::checkAccessDelegated()', 1, 'api4Actions');
$builder->addExpectedArguments('\Civi\API\EntityLookupTrait::define()', 0, 'api4Entities');