This class describes metadata that can be attached to an \ElggEntity. It is
rare that a plugin developer needs to use this API for metadata. Almost all
interaction with metadata occurs through the methods of \ElggEntity. See its
__set(), __get(), and setMetadata() methods.
public function testCanSaveMetadata() { $owner = $this->mocks()->getUser(); _elgg_services()->session->setLoggedInUser($owner); $object = $this->mocks()->getObject(['owner_guid' => $owner->guid]); $metadata = new ElggMetadata(); $metadata->entity_guid = $object->guid; $metadata->name = 'foo'; $metadata->value = 'bar'; $metadata->time_created = _elgg_services()->metadataTable->getCurrentTime()->getTimestamp(); $id = _elgg_services()->metadataTable->iterator + 1; // Insert $dbprefix = elgg_get_config('dbprefix'); $sql = "INSERT INTO {$dbprefix}metadata\n\t\t\t\t(entity_guid, name, value, value_type, owner_guid, time_created, access_id)\n\t\t\t\tVALUES (:entity_guid, :name, :value, :value_type, :owner_guid, :time_created, :access_id)"; _elgg_services()->db->addQuerySpec(['sql' => $sql, 'params' => [':entity_guid' => $metadata->entity_guid, ':name' => 'foo', ':value' => 'bar', ':value_type' => 'text', ':owner_guid' => $metadata->owner_guid, ':time_created' => $metadata->time_created, ':access_id' => $metadata->access_id], 'insert_id' => $id]); $this->assertEquals($id, $metadata->save()); _elgg_services()->session->removeLoggedInUser(); }
/** * Get exportable metadata values * * @param ElggMetadata $metadata * @return array */ function elgg_tokeninput_export_metadata($metadata) { if ($metadata instanceof ElggMetadata) { $type = $metadata->getType(); $subtype = $metadata->getSubtype(); $tag = $metadata->value; $id = $metadata->id; } else { if (is_string($metadata)) { $type = 'tag'; $subtype = null; $tag = $metadata; $id = null; } else { return array(); } } $export = array('label' => $tag, 'value' => $tag, 'type' => $type, 'subtype' => $subtype, 'html_result' => elgg_view_exists("tokeninput/{$type}/{$subtype}") ? elgg_view("tokeninput/{$type}/{$subtype}", array('tag' => $tag, 'metadata_id' => $id, 'for' => 'result')) : null, 'html_token' => elgg_view_exists("tokeninput/{$type}/{$subtype}") ? elgg_view("tokeninput/{$type}/{$subtype}", array('tag' => $tag, 'metadata_id' => $id, 'for' => 'token')) : null); $export = elgg_trigger_plugin_hook('tokeninput:entity:export', $type, array('tag' => $tag, 'metadata_id' => $id), $export); array_walk_recursive($export, function (&$value) { $value = is_string($value) ? html_entity_decode($value, ENT_QUOTES, 'UTF-8') : $value; }); return $export; }