View::make( string $view = null, Tribe__Context|null $context = null )

Builds and returns an instance of a View by slug or class.


Parameters

$view

(string) (Optional) The view slug, as registered in the tribe_events_views filter, or class.

Default value: null

$context

(Tribe__Context|null) (Optional) The context this view should render from; if not set then the global one will be used.

Default value: null


Top ↑

Return

(TribeEventsViewsV2View_Interface) An instance of the built view.


Top ↑

Source

File: src/Tribe/Views/V2/View.php

	public static function make( $view = null, \Tribe__Context $context = null ) {
		$view = null !== $view
			? $view
			: tribe_get_option( static::$option_default, 'default' );

		$views = self::get_registered_views();

		if ( 'default' === $view && count( $views ) ) {
			$view = reset( $views );
		}

		if ( class_exists( $view ) ) {
			$view_class = $view;
			$slug       = static::get_view_slug( $view );
		} else {
			$view_class = Arr::get( $views, $view, false );
			$slug       = $view;
		}

		$request_slug = $slug;

		if ( $view_class ) {
			if ( ! self::$container instanceof Container ) {
				$message = 'The ' . __CLASS__ . '::$container property is not set:'
				           . ' was the class initialized by the service provider?';
				throw new \RuntimeException( $message );
			}

			/** @var \Tribe\Events\Views\V2\View_Interface $instance */
			$instance = self::$container->make( $view_class );
		} else {
			$view_class = static::class;
			$instance   = new static();
			$slug       = 'not-found';
		}

		$template = new Template( $slug );

		/**
		 * Filters the Template object for a View.
		 *
		 * @since 4.9.3
		 *
		 * @param  \Tribe\Events\Views\V2\Template  $template  The template object for the View.
		 * @param  string                           $view      The current view slug.
		 * @param  \Tribe\Events\Views\V2\View      $instance  The current View object.
		 */
		$template = apply_filters( 'tribe_events_views_v2_view_template', $template, $view, $instance );

		/**
		 * Filters the Template object for a specific View.
		 *
		 * @since 4.9.3
		 *
		 * @param  \Tribe\Events\Views\V2\Template  $template  The template object for the View.
		 * @param  \Tribe\Events\Views\V2\View      $instance  The current View object.
		 */
		$template = apply_filters( "tribe_events_views_v2_{$slug}_view_template", $template, $instance );

		// Set some defaults on the template.
		$template->set( 'view_class', $view_class );
		$template->set( 'request_slug', $request_slug );

		$instance->set_template( $template );
		$instance->set_slug( $slug );

		// Let's set the View context from either the global context or the provided one.
		$view_context = null === $context ? tribe_context() : $context;

		/**
		 * Filters the Context object for a View.
		 *
		 * @since 4.9.3
		 *
		 * @param  \Tribe__Context  $view_context  The context abstraction object that will be passed to the
		 *                                         view.
		 * @param  string                       $view          The current view slug.
		 * @param  \Tribe\Events\Views\V2\View  $instance      The current View object.
		 */
		$view_context = apply_filters( 'tribe_events_views_v2_view_context', $view_context, $view, $instance );

		/**
		 * Filters the Context object for a specific View.
		 *
		 * @since 4.9.3
		 *
		 * @param  \Tribe__Context              $view_context  The context abstraction object that will be passed to the
		 *                                                     view.
		 * @param  \Tribe\Events\Views\V2\View  $instance      The current View object.
		 */
		$view_context = apply_filters( "tribe_events_views_v2_{$slug}_view_context", $view_context, $instance );

		$instance->set_context( $view_context );

		$view_repository = tribe_events();

		/**
		 * Filters the Repository object for a View.
		 *
		 * @since 4.9.3
		 *
		 * @param \Tribe__Repository__Interface $view_repository The repository instance the View will use.
		 * @param string                        $view            The current view slug.
		 * @param \Tribe\Events\Views\V2\View   $instance        The current View object.
		 */
		$view_repository = apply_filters( 'tribe_events_views_v2_view_context', $view_repository, $view, $instance );

		/**
		 * Filters the Repository object for a specific View.
		 *
		 * @since 4.9.3
		 *
		 * @param \Tribe__Repository__Interface $view_repository The repository instance the View will use.
		 * @param \Tribe\Events\Views\V2\View   $instance        The current View object.
		 */
		$view_repository = apply_filters( "tribe_events_views_v2_{$slug}_view_context", $view_repository, $instance );

		$instance->set_repository( $view_repository );

		$instance->set_url();

		return $instance;
	}

Top ↑

Changelog

Changelog
Version Description
4.9.2 Introduced.