Jump to content

Recommended Posts

Posted

Hi,

I'm facing an issue with no idea to solve it, even after a look on the forum and search engines.

I want to output each language available for a specific field on my page but without using a foreach loop. When I try to use setOutputFormatting() function it shows this error message:

Error: Uncaught Error: Call to a member function setOutputFormatting() on boolean [...]

For some reason I can't use a foreach function, I'm calling each page with a conditionnal incrementation of $i ($article[$i]). It seems the issue come from here but is there a way to bypass this?

Tell me if you need more information,

Thanks in advance,

Étienne

Posted

Well, a little code would be useful. Is this in a regular frontend template, or inside your custom module? WIthout seeing some code, it's hard to say why you're not able to do a loop...

  • Like 1
Posted

Yes sorry for the lake of information.

It is in a frontend template but I use a recursive function with an incrementation of a int variable so I store my subpages in an array using

$article = $page->children;

and call them using

$article[$i];

 

Here is an except of the code:

  $parts = $page->children('template=parts');
  foreach ($parts as $i => $part) {
?>
  <section id="<?= $part->id ?>" class="part lvl2 <?php echo ($i%2 == 0 ? 'even' : 'odd'); ?>">
    <h1><?= $title ?></h1>
    <?php
      $lvl = 3;
      $i = 0;
      recursionLvl3($lvl, $i, $part, $article, $permalink);
    ?>
  </section>

<?php
  }
?>

The recursive function:

  function recursionLvl3($lvl, $i, $part, $article, $permalink){

    echo '<section class="lvl3">
            <article class="lvl4 '; echo ($permalink == $article[$i]->name ?  (strlen($article[$i]->title) > 0 ? 'big"' : '') : '"'); echo ($article[$i]->name ? ' id="' . $article[$i]->name . '"' : '"'); echo'>';
              createArticle($article[$i]);
    echo '  </article>
          </section> <!-- lvl3 -->';
    $i++;
    echo '<section class="lvl3">';
    echo '  <section class="lvl4">';
    echo  '   <article class="lvl5 '; echo ($permalink == $article[$i]->name ?  (strlen($article[$i]->title) > 0 ? 'big"' : '') : '"'); echo ($article[$i]->name ? ' id="' . $article[$i]->name . '"' : '"'); echo '>';
                createArticle($article[$i]);
    echo '    </article>';
    echo '  </section> <!-- lvl4 -->';
    $i++;
    echo '  <section class="lvl4">';
    $lvl++;
    if ($lvl > 0 && $i < count($part->children)) {
      recursionLvl3($lvl, $i, $part,  $article, $permalink);
    }
    echo '  </section> <!-- lvl4 -->';

    echo '</section> <!-- lvl3 -->';
  }

and createArticle function:

  function createArticle($el){


    //
    // $el->setOutputFormatting(false);
    // $title = $el->title->getLanguageValue('default');
    // $titleFR = ($el->title->getLanguageValue('fr') ? $el->title->getLanguageValue('fr') : $title);
    // $titleNL = ($el->title->getLanguageValue('nl') ? $el->title->getLanguageValue('nl') : $title);

    echo '  <div class="close closeArticle"><span>×</span></div>';
    echo ($el->title ? '<h2>'. $el->title .'</h2>' : '');
    echo ($el->sous_titre ? '<h3>'. $el->sous_titre .'</h3>' : '');
    echo '<div class="dates">';
      echo ($el->date_debut ? '<h4 class="dateDebut"><span></span> ' . $el->date_debut . ' </h4>' : '');
      echo ($el->date_fin ? '<h4 class="dateFin"> <span>  ⤑  </span> ' . $el->date_fin . '</h4><br>' : '');
      echo ($el->vernissage ? '<h4 class="vernissage"> <span>vernissage&thinsp;: </span>' . $el->vernissage . '</h4>' : '');
    echo '</div>';
    echo ($el->images ? '<img src="' . $el->images->first()->url . '" alt="" />' : '' );
    echo '<div class="content">';
      echo ($el->contenu ? $el->contenu : '' );
    echo '</div>';
    echo ($el->en_savoir_plus ? '<p class="savoirPlus">' : '' );
      echo ($el->en_savoir_plus ? '<a href="' . $el->en_savoir_plus . '" target="_blank">En savoir plus</a>' : '' );
    echo ($el->en_savoir_plus ? '</p>' : '' );
  }

The commented lines are those which cause an issue.

 

Thank you,

Étienne

Posted (edited)

This worked for me. See the changes in getLanguageValue();

The echo's were too much for me :); I substituted them for $out (code be made even cleaner, but this is a start).

function createArticle($el){

	$out = '';

    //$el->setOutputFormatting(false);
    $title = $el->getLanguageValue('default', 'title');
    $titleFR = ($el->getLanguageValue('fr', 'title') ? $el->getLanguageValue('fr', 'title') : $title);
    $titleNL = ($el->getLanguageValue('nl', 'title') ? $el->getLanguageValue('nl', 'title') : $title);

    $out .= '  <div class="close closeArticle"><span>×</span></div>';
	//$out .= ($el->title ? '<h2>'. $el->title .'</h2>' : '');
	$out .= ($el->title ? '<h2>'. $titleFR .'</h2>' : '');// @note: just for testing
    $out .= ($el->sous_titre ? '<h3>'. $el->sous_titre .'</h3>' : '');
    $out .= '<div class="dates">';
	$out .= ($el->date_debut ? '<h4 class="dateDebut"><span></span> ' . $el->date_debut . ' </h4>' : '');
	$out .= ($el->date_fin ? '<h4 class="dateFin"> <span>  ⤑  </span> ' . $el->date_fin . '</h4><br>' : '');
	$out .= ($el->vernissage ? '<h4 class="vernissage"> <span>vernissage : </span>' . $el->vernissage . '</h4>' : '');
    $out .= '</div>';
    $out .= ($el->images ? '<img src="' . $el->images->first()->url . '" alt="" />' : '' );
    $out .= '<div class="content">';
	$out .= ($el->contenu ? $el->contenu : '' );
    $out .= '</div>';
    $out .= ($el->en_savoir_plus ? '<p class="savoirPlus">' : '' );
	$out .= ($el->en_savoir_plus ? '<a href="' . $el->en_savoir_plus . '" target="_blank">En savoir plus</a>' : '' );
	$out .= ($el->en_savoir_plus ? '</p>' : '' );
	
	return $out;

}

I have not looked at the rest of your code in detail...

Edited by kongondo
  • Like 2

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...