Collections functions
General functions
Node functions
Render functions
Theme permission functions
User functions
Resource functions

get_data_by_field()

Description

Get the resource data value for a field and a specific resource
or get the specified field for all resources in the system

records for the specified field

(for performance). Shouldn't be an issue as long as it's used in foreach loops

Parameters

ColumnTypeDefaultDescription
$resource integer Resource ID. Use NULL to retrieve all resources
$field integer|string Resource type field ID. Can also be a shortname.
$flatten bool true Should a fixed list field value be flatten to a simple string? Set to FALSE to get the list of nodes

Return

string|array|Generator Generator is returned for the old behaviour of returning field data for all resources

Location

include/resource_functions.php lines 7869 to 7911

Definition

 
function get_data_by_field($resource$fieldbool $flatten true)
    {
    global 
$get_data_by_field_fct_field_cache;

    
$fetch_all_resources is_null($resource);

    if(!isset(
$get_data_by_field_fct_field_cache[$field]))
        {
        
$rtf_info ps_query(
            
'SELECT ref, type FROM resource_type_field WHERE ref = ? OR name = ?',
            [
'i',$field's',$field],
            
'schema'
        
);

        if(empty(
$rtf_info))
            {
            return 
$fetch_all_resources ? [] : '';
            }

        
$get_data_by_field_fct_field_cache[$field] = $rtf_info[0];
        }

    
$rtf_ref $get_data_by_field_fct_field_cache[$field]['ref'];
    
$rtf_type $get_data_by_field_fct_field_cache[$field]['type'];

    if(!
$fetch_all_resources && $rtf_type == FIELD_TYPE_CATEGORY_TREE)
        {
        
$tree_nodes get_resource_nodes($resource$rtf_reftrue);
        return 
$flatten implode(', 'get_node_strings($tree_nodesfalse)) : $tree_nodes;
        }
    elseif(!
$fetch_all_resources)
        {
        
$resource_data_for_field get_resource_nodes($resource$rtf_reftrue);
        return 
$flatten implode(', 'array_column($resource_data_for_field'name')) : $resource_data_for_field;
        }
    
// Old behaviour from when we had resource_data (before r19945) - return the metadata field values for all resources
    
elseif($fetch_all_resources && in_array($rtf_typeNON_FIXED_LIST_SINGULAR_RESOURCE_VALUE_FIELD_TYPES))
        {
        return 
get_resources_nodes_by_rtf($rtf_ref);
        }

    return 
'';
    }

This article was last updated 19th March 2024 14:05 Europe/London time based on the source file dated 11th March 2024 14:25 Europe/London time.