Coding standards
Security in ResourceSpace
Developer reference
Database
Action functions
Admin functions
Ajax functions
Annotation functions
API functions
Collections functions
Comment functions
Config functions
CSV export functions
Dash functions
Debug functions
Encryption functions
Facial recognition functions
File functions
General functions
Language functions
Log functions
Login functions
Message functions
Migration functions
Node functions
PDF functions
Plugin functions
Render functions
Reporting functions
Request functions
Research functions
Slideshow functions
Theme permission functions
User functions
Video functions
Database functions
Metadata functions
Resource functions
Search functions
Map functions
Job functions
Tab functions
Test 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 8115 to 8157

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 17th November 2024 15:35 Europe/London time based on the source file dated 15th November 2024 18:30 Europe/London time.