How do I get metadata, extract values from it, and display the values with a simple custom format?

Hi,

I'm new to developing with Elgg and am trying to create a plugin.

I'm using the following command to get metadata pairs and want to output them in a list in the format of

<div>$label $url

which would display like

Google http://www.google.com

Yahoo http://www.yahoo.com


I'm not sure if this code is correct or not.

$metadata = elgg_get_entities_from_metadata(array(
    'types'=>'object',
    'subtypes'=>'preset_site',
    'metadata_name_value_pairs' => array(
        array('name' => 'label', 'value' => $label),
        array('name' => 'url', 'value' => $url)
    ),    
));   


So I have 2 questions.

1. Will this command do what I expect and store the meta data pairs in $metadata?

2. How do I extract individual values so I can echo them to html with my own formatting?

I've looked for a good reference on how to do this but have come up blank.

I tried elgg_list_entities_from_metadata($options); but it doesn't give me the layout I want.


Thanks!

  • Will this command do what I expect and store the meta data pairs in $metadata?

    No, it'l store entities

    $metadata = elgg_get_entities_from_metadata(array(
        'types'=>'object',
        'subtypes'=>'preset_site',
        'metadata_name_value_pairs' => array(
            array('name' => 'label', 'value' => $label),
            array('name' => 'url', 'value' => $url)
        ),    
    ));   

    This will get you all entities (in $metadata) were label == $label and url == $url.

    How do I extract individual values so I can echo them to html with my own formatting?

    You can then loop through $metadata like

    foreach ($metadata as $md) {

    echo $md->label;

    echo $md->url;

    }

    you can add your own layout as you wish.

    have you looked at http://learn.elgg.org/en/stable/tutorials/index.html

  • You can't store multiple key => value pairs under the same metadata name. You will have to create a new entity for each combination:

    $link = new ElggObject();
    $link->subtype = 'link';
    $link->access_id = ACCESS_PUBLIC;
    $link->label = $label;
    $link->address = $url;
    $link->save();
    
    $links = elgg_get_entities_from_metadata([
       'types' => 'object',
       'subtypes' => 'link',
       'metadata_name_value_pairs' => [
           'label' => $label,
           'address' => $url,
       ],
    ]);
    
    foreach ($links as $link) {
       echo $link->label . ': ' . $link->address . PHP_EOL;
    }
    
Beginning Developers

Beginning Developers

This space is for newcomers, who wish to build a new plugin or to customize an existing one to their liking