Getting the title of a resource stored in a property


#1

Hi,

(1) On “item/show” pages, when displaying a property which contains an Omeka Resource instead of Text, it shows a hyperlink that includes that resource’s title. Which function is used for that? I’ve dug through the source code and can’t figure it out. This is Omeka S 1.4.something (I’m using Reclaim Hosting, which doesn’t yet support S 2.0).

Related question (2)
I want to tweak the item browse page to add some additional columns of data. Is there a function similar to value() (or ideally, linkPretty()) that I can use to grab the titles of Omeka resources stored in a property? The only way I’ve been able to figure out how to do this is by using json_decode() to grab the json data for those resources, and then parse out the titles, and that’s definitely not the best way to go.

thanks!

Joe Easterly


#2

Hello @joeeasterly

Have a look at this sample code that should do exactly what your are looking for:

Have a nice day

Laurent


#3

Thanks Laurent! I wasn’t able to get the code to work: for some reason no matter what I did, it would only display dcterms:description for the current item, rather than dcterms:title of the linked items. However I was able to get the following code (bottom of this Omeka forum post) to work:

$auteurTerm = 'foaf:depicts';
$auteur = $item->value($auteurTerm) instanceof \Omeka\Api\Representation\ValueRepresentation
 ? $item->value($auteurTerm)->valueResource()->displayTitle() 
: $item->value($auteurTerm); 
if ($auteur): ?> <span><?php echo $auteur; ?></span>

Right now it’s getting just the first value. Is there a way to modify this so it supports a repeating field?

thanks again,

Joe


#4

You’re just trying to display the “normal” link we show for a Resource property?

You can just do echo $item->value($yourProperty)->asHtml();

asHtml() is the method we use to display values which automatically varies the display based on the type of value. If what you’re looking to produce is the actual complete link (or really, whatever the value “should” look like on a page), then asHtml() is what you want to be using.

As for single vs. multiple, value returns just the first value for a property by default. You can pass ['all' => true] as a second parameter to value() and it will instead return an array of all the matching values and you can just loop over that.


#5

This works, thanks!

N.B. for folks newer to php/omeka functions— if you pass ['all' => true] as the second parameter of value(), asHtml() will pitch a fit if you feed it an array. You have to do something like this:

$subjects = $item ->value('dcterms:subject', ['all' => true ]);
foreach ($subjects as $subject) {
          echo $subject->asHtml().' ';
        }