Pods::fields PHP Method

fields() public method

Return field array from a Pod, a field's data, or a field option
Since: 2.0
public fields ( null $field = null, null $option = null ) : boolean | mixed
$field null
$option null
return boolean | mixed
    public function fields($field = null, $option = null)
    {
        $field_data = null;
        if (empty($this->fields)) {
            // No fields found
            $field_data = array();
        } elseif (empty($field)) {
            // Return all fields
            $field_data = (array) $this->fields;
        } elseif (!isset($this->fields[$field]) && !isset($this->pod_data['object_fields'][$field])) {
            // Field not found
            $field_data = array();
        } elseif (empty($option)) {
            // Return all field data
            if (isset($this->fields[$field])) {
                $field_data = $this->fields[$field];
            } elseif (isset($this->pod_data['object_fields'])) {
                $field_data = $this->pod_data['object_fields'][$field];
            }
        } else {
            // Merge options
            if (isset($this->fields[$field])) {
                $options = array_merge($this->fields[$field], $this->fields[$field]['options']);
            } elseif (isset($this->pod_data['object_fields'])) {
                $options = array_merge($this->pod_data['object_fields'][$field], $this->pod_data['object_fields'][$field]['options']);
            }
            // Get a list of available items from a relationship field
            if ('data' == $option && in_array(pods_var_raw('type', $options), PodsForm::tableless_field_types())) {
                $field_data = PodsForm::field_method('pick', 'get_field_data', $options);
            } elseif (isset($options[$option])) {
                $field_data = $options[$option];
            }
        }
        /**
         * Modify the field data before returning
         *
         * @since unknown
         *
         * @param array $field_data The data for the field.
         * @param string|null $field The specific field that data is being return for, if set when method is called or null.
         * @param string|null $option Value of option param when method was called. Can be used to get a list of available items from a relationship field.
         * @param Pods|object $this The current Pods class instance.
         */
        return apply_filters('pods_pods_fields', $field_data, $field, $option, $this);
    }

Usage Example

 /**
  * Add fields, based on options to REST read/write requests
  *
  * @since  2.5.6
  *
  * @access protected
  */
 protected function add_fields()
 {
     $fields = $this->pod->fields();
     foreach ($fields as $field_name => $field) {
         $read = self::field_allowed_to_extend($field_name, $this->pod, 'read');
         $write = self::field_allowed_to_extend($field_name, $this->pod, 'write');
         $this->register($field_name, $read, $write);
     }
 }