Simple metadata problem..

I have a new object and I'm trying to add a metadata field to it. I've searched through the documentation, but whenever I try what it says it doesn't work for me. I feel like its a very simple task but I just can't figure out what I am doing wrong. I'm new to elgg, and made this plugin based off the pages plugin.

"type" is what I'm trying to add to the metadata, I understand there is a 'subtype' for the elgg object entity, but for my purposes I need this, and eventually will add more fields.

the form looks like:

_______________________

<div>

    <label><?php echo elgg_echo("title"); ?></label><br />

    <?php echo elgg_view('input/text',array('name' => 'title')); ?>

</div>

 

<div>

    <label><?php echo elgg_echo("body"); ?></label><br />

    <?php echo elgg_view('input/longtext',array('name' => 'body')); ?>

</div> 

 

<div>

    <label><?php echo elgg_echo("type"); ?></label><br />

    <?php echo elgg_view('input/text',array('name' => 'type')); ?>

</div> 

 

<div>

    <label><?php echo elgg_echo("tags"); ?></label><br />

    <?php echo elgg_view('input/tags',array('name' => 'tags')); ?>

</div>

 

<div>

    <?php echo elgg_view('input/submit', array('value' => elgg_echo('save'))); ?>

</div>

________________________

 

 

and then it goes to the action...

 

 

______________________

<?php

  // get the form inputs

  $title = get_input('title');

  $body = get_input('body');

  $tags = string_to_tag_array(get_input('tags'));

  $type = get_input('type')

 

  // create a new node object

  $network = new ElggObject();

  $network->subtype = "network";

  $network->title = $title;

  $network->description = $body;

  $network->type = $type;

  

 

  // for now make all nodes public

  $network->access_id = ACCESS_PUBLIC;

 

  // owner is logged in user

  $network->owner_guid = elgg_get_logged_in_user_guid();

 

  // save tags as metadata

  $network->tags = $tags;

 

  // save to database and get id of the new node

  $network_guid = $network->save();

  

 

  // if the network was saved, we want to display the new post

  // otherwise, we want to register an error and forward back to the form

  if ($network_guid) {

     system_message("Your network was created");

     forward($network->getURL());

  } else {

     register_error("The network could not be created");

     forward(REFERER); // REFERER is a global variable that defines the previous page

  }

?>

________________________

 

 

If I take the meta data information out the form works just fine, so I'm just sure if I need to set up the metadata, register it, or if I'm just coding it wrong... any input would be great, I'm sure this is a basic thing for most users, but I'm learning php and the elgg engine right now, I just have HTML, CSS, and a little java knowledge.

 

Brad

 

  • 'type' is a reserved attribute name. Each entity has a type ('object', 'user', 'group' or 'site'), and any 'subtype'. 

    You won't be able to use type as metadata. If you change 'type' to something other than the above 4, you will break your site.

    Use a different metadata name, e.g. mytype

  • hmm still missing something, I changed the metadata name to 'nodetype' everything in the above  post is the same aside from that. any other ideas?

  • Ok, so I've been able to save the objects (presumably with the metadata), so I'm trying to create a page that will list just the objects with 'business' for metadata type 'nodetype'

     

    this is how my action page looks (storing the metadata from the form listed above)...

     

    ________________________

    <?php

      // get the form inputs

      $title = get_input('title');

      $body = get_input('body');

      $tags = string_to_tag_array(get_input('tags'));

     

      // create a new node object

      $network = new ElggObject();

      $network->subtype = "network";

      $network->title = $title;

      $network->description = $body;

      

      $network->nodetype = get_input('nodetype');

      

     

      // for now make all nodes public

      $network->access_id = ACCESS_PUBLIC;

     

      // owner is logged in user

      $network->owner_guid = elgg_get_logged_in_user_guid();

     

      // save tags as metadata

      $network->tags = $tags;

     

      // save to database and get id of the new node

      $network_guid = $network->save();

      

     

      // if the network was saved, we want to display the new post

      // otherwise, we want to register an error and forward back to the form

      if ($network_guid) {

         system_message("Your network was created");

         forward($network->getURL());

      } else {

         register_error("The network could not be created");

         forward(REFERER); // REFERER is a global variable that defines the previous page

      }

    ?>

    ___________________________

     

    here is my coding for the page to list just objects with 'business' for metadata 'nodetype'

     

    _____________________________

    <?php

    $body = elgg_list_entities(array(

        'type' => 'object',

        'subtype' => 'network',

        'nodetype' => 'business',

    ));

     

    $body = elgg_view_layout('one_column', array('content' => $body));

     

    echo elgg_view_page("Business Nodes", $body);

    ?>

     

    ________________

     

    When I do that I get all objects of subtype 'network' so I'm not filtering it quite right.

     

    so then I tried..

     

    ________________

     

    <?php

    $body = elgg_get_entities_from_metadata($nodetype => 'business');

     

    $body = elgg_view_layout('one_column', array('content' => $body));

     

    echo elgg_view_page("All Networks", $body);

    ?>

    __________________

     

    that returns a 500 error. I have a page that will list all objects of subtype node, I just don't know how to filter it to just the metadata type 'nodetype' with value 'business'

     

    for reference here is my list all page:

    ____________________

    <?php

    $body = elgg_list_entities(array(

        'type' => 'object',

        'subtype' => 'network',

    ));

     

    $body = elgg_view_layout('one_column', array('content' => $body));

     

    echo elgg_view_page("All Networks", $body);

    ?>

    _________________

     

    Any help would be greatly appreciated. Cheers.

  • You were on the right track with

    $body = elgg_get_entities_from_metadata($nodetype => 'business');

    Here's the documentation for that function: http://reference.elgg.org/1.8/engine_2lib_2metadata_8php.html#aec4b6f0b9565e3554acb9b39ef34a2ac

     

    Try

    $body = elgg_get_entities_from_metadata('metadata_names' => array('nodetype'), 'metadata_values' => array('business'));

  • hmm no luck.. i tried:

     

    _________________

    <?php

     

    $body = elgg_get_entities_from_metadata('metadata_names' => array('nodetype'), 'metadata_values' => array('business'));

     

    $body = elgg_view_layout('one_column', array('content' => $body));

     

    echo elgg_view_page("Business Nodes", $body);

    ?>

     

    _________________

    which returned a 500 error, so i tried a few variations by adding in the list tag underneath the new tag

    ________________

     

     

    <?php

     

    elgg_get_entities_from_metadata('metadata_names' => array('nodetype'), 'metadata_values' => array('business'));

     

    $body = elgg_list_entities(array('business'));

     

    $body = elgg_view_layout('one_column', array('content' => $body));

     

    echo elgg_view_page("Business Nodes", $body);

    ?>

    ______

     

    <?php

     

    $body = elgg_get_entities_from_metadata('metadata_names' => array('nodetype'), 'metadata_values' => array('business'));

     

    $body = elgg_list_entities(array('business'));

     

    $body = elgg_view_layout('one_column', array('content' => $body));

     

    echo elgg_view_page("Business Nodes", $body);

    ?>

    ________

     

    <?php

     

    $body = elgg_get_entities_from_metadata('metadata_names' => array('nodetype'), 'metadata_values' => array('business'));

     

    elgg_list_entities(array('business'));

     

    $body = elgg_view_layout('one_column', array('content' => $body));

     

    echo elgg_view_page("Business Nodes", $body);

    ?>

    ________

    I'm not sure if the error comes from the page being drawn, or maybe because the metadata isn't storing correctly, but i would think that would just draw a page with the title and no list of entities..

     

    other places to check for errors?

    -thanks for your response matt

     

     

     

  • elgg_list_entities_from_metadata(array(
                                    'metadata_name' => 'nodetype',
                                    'metadata_value' => 'business',
                                    'type' => 'object',
                                    'subtype' => 'network',
                                    'limit' => 10
                                    ));

  • thank you that helped, now the page loads, but there are no entries, just a blank page with the header/footer/etc so I must be storing the metadata incorrectly..

    I have a form that passes to an action, here 

     

    Form:

    ___________________

    <div>

        <label><?php echo elgg_echo("title"); ?></label><br />

        <?php echo elgg_view('input/text',array('name' => 'title')); ?>

    </div>

     

    <div>

        <label><?php echo elgg_echo("body"); ?></label><br />

        <?php echo elgg_view('input/longtext',array('name' => 'body')); ?>

    </div> 

     

    <div>

        <label><?php echo elgg_echo("nodetype"); ?></label><br />

        <?php echo elgg_view('input/text',array('name' => 'nodetype')); ?>

    </div> 

     

    <div>

        <label><?php echo elgg_echo("tags"); ?></label><br />

        <?php echo elgg_view('input/tags',array('name' => 'tags')); ?>

    </div>

     

    <div>

        <?php echo elgg_view('input/submit', array('value' => elgg_echo('save'))); ?>

    </div>

    ______________

     

    Action:

    _____________

    <?php

      // get the form inputs

      $title = get_input('title');

      $body = get_input('body');

      $tags = string_to_tag_array(get_input('tags'));

      $nodetype = get_input('nodetype');

      

      // create a new node object

      $network = new ElggObject();

      $network->subtype = "network";

      $network->title = $title;

      $network->description = $body;

      $network->nodetype = $nodetype;

      

     

      // for now make all nodes public

      $network->access_id = ACCESS_PUBLIC;

     

      // owner is logged in user

      $network->owner_guid = elgg_get_logged_in_user_guid();

     

      // save tags as metadata

      $network->tags = $tags;

     

      // save to database and get id of the new node

      $network_guid = $network->save();

      

     

      // if the network was saved, we want to display the new post

      // otherwise, we want to register an error and forward back to the form

      if ($network_guid) {

         system_message("Your network was created");

         forward($network->getURL());

      } else {

         register_error("The network could not be created");

         forward(REFERER); // REFERER is a global variable that defines the previous page

      }

    ?>

    _______________

     

    I'm not sure if I need to somehow register meta data variable before i can save to it. I've tried using the create metadata function but couldn't figure out the right context. The documentation I've seen makes it seem like you don't need to do anything before hand... any suggestions?

     

     

  • $params = array(
                'metadata_name' => 'nodetype',
                'metadata_value' => 'business',
                'types' => 'object',
                'subtypes' => 'network',
                'limit' => 10
            );

    echo elgg_list_entities_from_metadata($params);

  • YES! Thank you very much ElggCloud, and everyone else who posted. The final ..

     

    _________

    <?php

     

    $params = array(

                'metadata_name' => 'nodetype',

                'metadata_value' => 'business',

                'types' => 'object',

                'subtypes' => 'network',

                'limit' => 10

            );

     

    $body = elgg_list_entities_from_metadata($params);

     

    $body = elgg_view_layout('one_column', array('content' => $body));

     

    echo elgg_view_page("Business Nodes", $body);

    ?>

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