Controller function with multiple returns

I have this in my Page controller:

public static function pageHeaderImage()
{
if ( (get_field( 'page_header_style') == "Quote" || get_field( 'page_header_style') == "Basic") && get_field( 'show_featured_image') == "Yes") {
  the_post_thumbnail('w890h500');
}
return;
}

Which is accessed in a partial:

<div class="d-flex align-items-center">
  @php Page::pageHeaderImage() @endphp
  @include('partials.page-header-text', [
    'subtitle' => $page_header_subtitle,
    'title' => $page_header_title,
    'text' => $page_header_text,
  ])
</div>

I’d like to pass through an additional value in that function, like this:

public static function pageHeaderImage()
{
if ( (get_field( 'page_header_style') == "Quote" || get_field( 'page_header_style') == "Basic") && get_field( 'show_featured_image') == "Yes") {
  the_post_thumbnail('w890h500');
  **$class = 'has-img';**
}
return;
}

Then access that variable in my partial, like:

<div class="d-flex align-items-center **{{ $class }}**">
  @php Page::pageHeaderImage() @endphp
  @include('partials.page-header-text', [
    'subtitle' => $page_header_subtitle,
    'title' => $page_header_title,
    'text' => $page_header_text,
  ])
</div>

I have experimented with returning an array from the fuction like:

public static function pageHeaderImage($header_img)
{
if ( (get_field( 'page_header_style') == "Quote" || get_field( 'page_header_style') == "Basic") && get_field( 'show_featured_image') == "Yes") {
  $image = get_post_thumbnail('w890h500');
  $class = 'has-img';
}
return array($image, $class);
}

Then attempting to access in the partial like:

<div class="d-flex align-items-center **@php list($class) = Page::pageHeaderImage($header_img) @endphp**">
  **@php list($image) = Page::pageHeaderImage($header_img) @endphp**
  @include('partials.page-header-text', [
    'subtitle' => $page_header_subtitle,
    'title' => $page_header_title,
    'text' => $page_header_text,
  ])
</div>

If I dump the function call:

@dump(list($image) = Page::pageHeaderImage($header_img))

If it returns true I get:

array(2) { [0]=> string(0) "" [1]=> string(7) "has-img" }

If it returns false I get:

array(2) { [0]=> NULL [1]=> NULL }

The issue i’m having is how to output those variables, and how to do it in the cleanest Sage way.

Thanks

Unless you have some reason to use a static method specifically, I would remove the static from your method definition so you can access it as a simple variable in your blades. I.e.:

// Controller
public function pageHeaderImage()

// Blade
$page_header_image

Beyond that, I usually like to pass multiple values by casting an array to an object (it looks nicer and is a bit easier to write):

// Controller
public function pageHeaderImage()
{
   return (object) [
      'class' => 'has-img',
      'img' => get_the_post_thumbnail(),
   ];
}

// Blade
{{ $page_header_image->class }} // has-img
{!! $page_header_image->img !}} // Image element
1 Like

That’s perfect, thank you. This is what I ended up with:

Controller

public function pageHeaderImage()
{
    if ( (get_field( 'page_header_style') == "Quote" || get_field( 'page_header_style') == "Basic") && get_field( 'show_featured_image') == "Yes") {
      return (object) [
        'class' => 'has-img',
        'img' => wp_get_attachment_image(get_post_thumbnail_id( get_the_ID() ),'w890h500'),
      ];
    }
}

Blade

<div class="d-flex align-items-center {{ $page_header_image->class }}">

  {!! $page_header_image->img !!}

  @include('partials.page-header-text', [
    'subtitle' => $page_header_subtitle,
    'title' => $page_header_title,
    'text' => $page_header_text,
  ])

</div>

This topic was automatically closed after 42 days. New replies are no longer allowed.