wpseek.com
A WordPress-centric search engine for devs and theme authors



wp_get_entity_view_config › WordPress Function

Since7.1.0
Deprecatedn/a
wp_get_entity_view_config ( $kind, $name )
Parameters: (2)
  • (string) $kind The entity kind (e.g. `postType`).
    Required: Yes
  • (string) $name The entity name (e.g. `page`).
    Required: Yes
Returns:
  • (array) {
    The view configuration for the entity.

    @type array $default_view Default view configuration.
    @type array $default_layouts Default layouts configuration.
    @type array $view_list List of available views.
    @type array $form Form configuration.
    }
Defined at:
Codex:

Returns the view configuration for the given entity.

Builds the default configuration shared by all entities and then exposes it through the dynamic get_entity_view_config_{$kind}_{$name} filter so that core and third parties can provide the configuration for a specific entity.


Source

function wp_get_entity_view_config( $kind, $name ) {
	$default_view    = array(
		'type'       => 'table',
		'filters'    => array(),
		'sort'       => array(
			'field'     => 'title',
			'direction' => 'asc',
		),
		'perPage'    => 20,
		'fields'     => array( 'author', 'status' ),
		'titleField' => 'title',
	);
	$default_layouts = array(
		'table' => array(),
		'grid'  => array(),
		'list'  => array(),
	);
	$all_items_title = __( 'All items' );
	if ( 'postType' === $kind ) {
		$post_type_object = get_post_type_object( $name );
		if ( $post_type_object && ! empty( $post_type_object->labels->all_items ) ) {
			$all_items_title = $post_type_object->labels->all_items;
		}
	}
	$view_list = array(
		array(
			'title' => $all_items_title,
			'slug'  => 'all',
		),
	);

	$config = array(
		'default_view'    => $default_view,
		'default_layouts' => $default_layouts,
		'view_list'       => $view_list,
		'form'            => 'postType' === $kind ? _wp_get_default_post_type_form() : array(),
	);

	$data = new WP_View_Config_Data( $config );

	/**
	 * Filters the view configuration for a given entity.
	 *
	 * The dynamic portions of the hook name, `$kind` and `$name`, refer to the
	 * entity kind (e.g. `postType`) and the entity name (e.g. `page`).
	 *
	 * Callbacks receive a WP_View_Config_Data object and change the
	 * configuration through its methods: the `update_*()` methods merge
	 * partial changes into the current configuration, while `set()` replaces
	 * a whole top-level key. Callbacks must return the object they were
	 * given.
	 *
	 * @since 7.1.0
	 *
	 * @param WP_View_Config_Data $data   The view configuration container
	 *                                    for the entity, exposing the
	 *                                    `default_view`, `default_layouts`,
	 *                                    `view_list`, and `form` keys.
	 * @param array               $entity {
	 *     The entity the configuration is built for.
	 *
	 *     @type string $kind The entity kind.
	 *     @type string $name The entity name.
	 * }
	 */
	$filtered = apply_filters(
		"get_entity_view_config_{$kind}_{$name}",
		$data,
		array(
			'kind' => $kind,
			'name' => $name,
		)
	);

	// A well-behaved callback returns the object it was given. Fall back to the
	// unfiltered config if a callback replaced it with something else.
	if ( ! $filtered instanceof WP_View_Config_Data ) {
		_doing_it_wrong(
			__FUNCTION__,
			sprintf(
				/* translators: %s: the filter hook name. */
				esc_html__( 'A "%s" filter callback must return the WP_View_Config_Data object it was given.' ),
				esc_html( "get_entity_view_config_{$kind}_{$name}" )
			),
			'7.1.0'
		);
		return $config;
	}

	// Backfill any dropped keys with their defaults, then discard any keys the
	// filter introduced that are not part of the documented configuration shape.
	return array_intersect_key( array_merge( $config, $filtered->get_config() ), $config );
}