folbert / fewbricks

Write code to create ACF field groups, fields and re-usable modules.
https://fewbricks2.folbert.com
GNU General Public License v3.0
124 stars 12 forks source link

Add function get_field_values to brick-class #25

Closed folbert closed 7 years ago

folbert commented 7 years ago

This will allow for writing $data = $this->get_field_values(['field_name', 'another_field_name]);

instead of

$data['field_name'] = $this->get_field('field_name');
$data['another_field_name'] = $this->get_field('another_field_name');

Code:

/**
     * @param $field_names
     * @return array
     */
    protected function get_field_values($field_names)
    {

        $values = [];

        foreach($field_names AS $field_name) {
            $values[$field_name] = $this->get_field($field_name);
        }

        return $values;

    }
folbert commented 7 years ago

Improved version allowing you to pass an multidimensional array to save a field name under another name in the returned array.

    /**
     * @param array $field_names ['field_name_1', 'field_name_2', ['field_name_3' => 'name_to_save_as']]
     * @return array
     */
    protected function get_field_values($field_names)
    {

        $values = [];

        foreach($field_names AS $field_name) {

            if(is_array($field_name)) {
                $key = key($field_name);
                $values[$field_name[$key]] = $this->get_field($key);
            } else {
                $values[$field_name] = $this->get_field($field_name);
            }
        }

        return $values;

    }
folbert commented 7 years ago

Also add way to pass if value should be fetched from options. Or maybe implement separate function for that.