Carbon_Fields\Helper\Helper::parse_relationship_field PHP Метод

parse_relationship_field() публичный статический Метод

Parse the raw value of the relationship and association fields.
public static parse_relationship_field ( string $raw_value = '', string $type = '' ) : array
$raw_value string Raw relationship value.
$type string Field type.
Результат array Array of parsed data.
    public static function parse_relationship_field($raw_value = '', $type = '')
    {
        if ($raw_value && is_array($raw_value)) {
            $value = array();
            foreach ($raw_value as $raw_value_item) {
                if (is_string($raw_value_item) && strpos($raw_value_item, ':') !== false) {
                    $item_data = explode(':', $raw_value_item);
                    $item = array('id' => $item_data[2], 'type' => $item_data[0]);
                    if ($item_data[0] === 'post') {
                        $item['post_type'] = $item_data[1];
                    } elseif ($item_data[0] === 'term') {
                        $item['taxonomy'] = $item_data[1];
                    }
                    $value[] = $item;
                } elseif ($type === 'association') {
                    $value[] = array('id' => $raw_value_item, 'type' => 'post', 'post_type' => get_post_type($raw_value_item));
                } else {
                    $value[] = $raw_value_item;
                }
            }
            $raw_value = $value;
        }
        return $raw_value;
    }

Usage Example

Пример #1
0
 /**
  * Echoes the widget content.
  * Sub-classes can over-ride this method to generate their widget code
  * but it is best to override front_end().
  *
  * @param array $args     Display arguments including 'before_title', 'after_title',
  *                        'before_widget', and 'after_widget'.
  * @param array $instance The settings for the particular instance of the widget.
  */
 public function widget($args, $instance)
 {
     // prepare $instance values for complex fields
     if (!empty($this->complex_field_names)) {
         $instance = self::unwrap_complex_field_values($instance, $this->complex_field_names);
     }
     // prepare $instance values for association fields
     foreach ($instance as &$field_value) {
         $field_value = Helper::parse_relationship_field($field_value);
     }
     // output
     if ($this->print_wrappers) {
         echo $args['before_widget'];
     }
     $this->front_end($args, $instance);
     if ($this->print_wrappers) {
         echo $args['after_widget'];
     }
 }