Condition php code to itemSet

I am trying to display differently two collections (item sets) by editing the browse.phtml file.

I have this code (thanks to pols12) but it generates an error:

<?php
$isAuteur = false;
foreach($item->itemSets() as $set)
  if($set->id() == 6723)
    $isAuteur = true;

if($isAuteur):
?>

My code for item set 6723


<?php endif; ?>


Any idea why?

Avoid to use short coding, perhaps it will fix it:

<?php
$isAuteur = false;
foreach ($item->itemSets() as $set):
  if ($set->id() == 6723):
    $isAuteur = true;
    break;
  endif;
endforeach;

if($isAuteur):
?>
    My code for item set 6723
<?php endif; ?>

And this process doesn’t work cleanly when there are multiple collections for an item.

Thanks !

I have the same error with your code.

However, it works when I place it at the bottom of the page. I have another instance of ‘foreach’. It seems if I place the condition code before, I have an error, after it is ok.

The whole test browse page with the code at the top -> it doesn't work
<?php $escape = $this->plugin('escapeHtml');?>
<?php $this->htmlElement('body')->appendAttribute('class', 'item resource browse');
$query = $this->params()->fromQuery(); ?>
<?php if (isset($itemSet)): ?>
<?php $this->htmlElement('body')->appendAttribute('class', 'item-set');
    $query['item_set_id'] = $itemSet->id(); ?>
<?php endif; ?>

<?php if (isset($itemSet)): ?>
    <?php echo $this->pageTitle($itemSet->displayTitle(), 2); ?>
    <div class="metadata">
        <?php echo $itemSet->displayValues(); ?>
    </div>
    <?php echo $this->pageTitle($this->translate('Items'), 3); ?>
<?php else: ?>
    <?php echo $this->pageTitle($this->translate('Items'), 2); ?>
<?php endif; ?>

<?php echo $this->searchFilters(); ?>

<?php  echo $this->hyperlink($this->translate('Advanced search'), $this->url('site/resource', ['controller' => 'item', 'action' => 'search'], ['query' => $query], true), ['class' => 'advanced-search']);  ?>  

<?php echo $this->pagination(); ?>

<?php
$isAuteur = false;
foreach ($item->itemSets() as $set):
  if ($set->id() == 6723):
    $isAuteur = true;
    break;
  endif;
endforeach;

if($isAuteur):
?>

test

<?php endif; ?>


	<?php foreach ($items as $item): ?>
	 
		<?php echo $item->link($item->displayTitle()); ?>

	<?php endforeach; ?>

</ul>


<?php echo $this->pagination(); ?>
<?php $this->trigger('view.browse.after'); ?>
The whole test browse page with the code at the bottom -> it works
<?php $escape = $this->plugin('escapeHtml');?>
<?php $this->htmlElement('body')->appendAttribute('class', 'item resource browse');
$query = $this->params()->fromQuery(); ?>
<?php if (isset($itemSet)): ?>
<?php $this->htmlElement('body')->appendAttribute('class', 'item-set');
    $query['item_set_id'] = $itemSet->id(); ?>
<?php endif; ?>

<?php if (isset($itemSet)): ?>
    <?php echo $this->pageTitle($itemSet->displayTitle(), 2); ?>
    <div class="metadata">
        <?php echo $itemSet->displayValues(); ?>
    </div>
    <?php echo $this->pageTitle($this->translate('Items'), 3); ?>
<?php else: ?>
    <?php echo $this->pageTitle($this->translate('Items'), 2); ?>
<?php endif; ?>

<?php echo $this->searchFilters(); ?>

<?php  echo $this->hyperlink($this->translate('Advanced search'), $this->url('site/resource', ['controller' => 'item', 'action' => 'search'], ['query' => $query], true), ['class' => 'advanced-search']);  ?>  

<?php echo $this->pagination(); ?>


	<?php foreach ($items as $item): ?>
	 
		<?php echo $item->link($item->displayTitle()); ?>

	<?php endforeach; ?>

</ul>

<?php
$isAuteur = false;
foreach ($item->itemSets() as $set):
  if ($set->id() == 6723):
    $isAuteur = true;
    break;
  endif;
endforeach;

if($isAuteur):
?>

test

<?php endif; ?>


<?php echo $this->pagination(); ?>
<?php $this->trigger('view.browse.after'); ?>

The error is the following: Call to a member function itemSets() on null.

It means that the item is null, so you may have to check it before.

The two item set I use (6723 and 6717) exist and have content.

But if I go to “/s/site/item-set” (see all collections/item set), I have none!

I renamed the two collections I have. Could it be the reason? How can I reindex them?

You don’t need to reindex, there is no index on Omeka S. You have to attach all the item sets to each site in the menu Resources of each site.

Ok, I found this option and added my two item sets to my site (before I added items by collection in the first tab).

So now the page “/s/site/item-set” shows my two collections.

However, it does not change my problem. I created the simplest browse.phtml page to test. According to this page, it I browse an articles list, it should print “test” and if it is an author list, it should print “test2”. But it produces only " Call to a member function itemSets() on null".

<?php
$isArticle = false;
foreach ($item->itemSets() as $set):
if ($set->id() == 6723):
$isArticle = true;
break;
endif;
endforeach;

if($isArticle):
?>

test

<?php endif; ?>


<?php
$isAuteur = false;
foreach ($item->itemSets() as $set):
if ($set->id() === 6717):
$isAuteur = true;
break;
endif;
endforeach;

if($isAuteur):
?>

test2

<?php endif; ?>