Getting Child Collection metadata - call to get_collection_by_id causes error

Hi there,

I’m modifying a theme’s collection/show page so that top-level collections are differentiated from child collections in their formatting. Top-level collections display a grid of child collections and child collections display items in a grid. In order to return metadata on a collection I have re-used the function presented in the old forum topic ‘Collection Tree: getting child collections’.

This function seems to work fine except when I try to call get_collection_by_id whereupon I get …

Fatal error : Call to undefined function get_collection_by_id()

… my code is as follows …

function Leeds_GATE_get_child_collections($collectionId) {
	if(plugin_is_active('CollectionTree')) {
            $treeChildren = get_db()->getTable('CollectionTree')->getChildCollections($collectionId);
            $childCollections = array();
            foreach($treeChildren as $treeChild) {
		
		echo '<li class="collection record">';
		
		$linkid = ($treeChild['id']);
		$linktext = ($treeChild['name']);

		echo "<a href='$linkid'>$linktext</a>";
		echo '<div class="img">';
		get_collection_image($linkid);
		echo '</div>';
		echo '</li>';


Causes error ----------->	$childCollections[] = get_collection_by_id($treeChild['id']);
		
            }
            return $childCollections;

	}
	return array();
    }


    $collectionId = metadata('collection', 'id'); 
    Leeds_GATE_get_child_collections($collectionId);

… I know the function is partly working as it is able to echo back data in the ‘CollectionTree’ table.

Any help would be appreciated.

Stephen

That’s quite an old forum topic.

get_collection_by_id doesn’t exist anymore, instead it’s get_record_by_id('collection', $id) (passing the ID you’re looking for as $id of course).

This and many other function name changes are listed on the “What’s new in 2.0” developer documentation.

1 Like

Thanks for the swift response.

Yes replacing the defunct ‘get_collection_by_id’ with ‘get_record_by_id(‘collection’, $id)’ worked. Thanks for the pointers to “What’s new in 2.0” developer documentation.

For reference the revised code which now works fine is as follows …

  <?php 

    function Leeds_GATE_get_child_collections($collectionId) {
	if(plugin_is_active('CollectionTree')) {
            $treeChildren = get_db()->getTable('CollectionTree')->getChildCollections($collectionId);
            $childCollections = array();
            foreach($treeChildren as $treeChild) {
		
		echo '<li class="collection record">';
		
		$linkid = ($treeChild['id']);
		
		set_current_record('collection', get_record_by_id('collection', $linkid));

		$linktext = metadata('collection', array('Leeds-GATE element set', 'GATE Title'));

		echo "<a href='$linkid'>$linktext</a>";

		echo '<div class="img">';
		get_collection_image($linkid);

		echo '</div>';
		echo '</li>';
		
            }
            return $childCollections;

	}
	return array();
    }

    $collectionId = metadata('collection', 'id'); 
    Leeds_GATE_get_child_collections($collectionId); 

    ?>

All the best

Stephen

1 Like