2

I've been working on developing my own theme and had to make a few custom blocks with ACF using my Laravel Sage environment.

My objective is to retrieve a list of text from a repeater object. (which successfully works, but the error is still displayed).

So the code that is throwing that error is as follows:

page.blade.php:

@php
  $tv_lists = [];
  while (have_rows('tv_lists')) {
    the_row();
    $tv_lists[] = [
      'tv_list' => get_sub_field('tv_list'),
    ];
  }
@endphp

@if(get_field('tv_lists'))
          <div class="tv-lists col-12 col-md-8 col-lg-9 block-image">
            @foreach($tv_lists as $tv_list)
              @if(!empty($tv_list['tv_list']))
                <div class="tv-list quote-content">
                  <a class="tv-list-text">{{ $tv_list['tv_list'] }}</a>
                </div>
              @endif
            @endforeach
          </div>
        @endif

And then the PHP code that's responsible for creating the repeater blocks is as follows in page.php:

->addRepeater('tv_lists', [
    'conditional_logic' => [[['field' => 'links_type', 'operator' => '==', 'value' => 'list']]],
    'label' => 'TV Lists',
    'layout' => 'block',
  ])
    ->addText('tv_list', [
      'label' => 'TV List',
    ])
    ->endRepeater()

Thank you in advance to anyone that's taking the time to read through this.

Samuel Lelièvre
  • 3,212
  • 1
  • 14
  • 27
zadders
  • 428
  • 1
  • 8
  • 30
  • 1
    Probably you need an `if(get_field('tv_list'))` before the while – Howard E Sep 17 '20 at 10:07
  • @HowardE When I put that if statement, the list no longer shows where it's supposed to and the error is still displayed. – zadders Sep 17 '20 at 10:11
  • @HowardE Actually Once I added the if statement to both fields, it worked. And it also had to be tv_lists instead of list. same for links. You should respond as an official answer so I Can vote it as closed :) – zadders Sep 17 '20 at 13:12

1 Answers1

1

You need an if statement before the while

so you want to do something like this.

@php
  $tv_lists = [];
  if (have_rows('tv_lists'){
    while (have_rows('tv_lists')) {
      the_row();
      $tv_lists[] = [
        'tv_list' => get_sub_field('tv_list'),
      ];
    }
  }
@endphp
Howard E
  • 5,454
  • 3
  • 15
  • 24
  • Thank you very much! This worked like a charm to solve this error! Because I was trying to add 1 of 2 options, when 1 was added, the other was empty, vice versa. So I had to make sure that it checks if it has the rows as opposed to assuming both should exist. – zadders Sep 18 '20 at 15:13