public function handle(\ElggEntity $entity) {

		$shortname = $this->getShortname();

		if ($entity->guid) {
			$current_metadata = elgg_get_metadata(array(
				'guids' => (int) $entity->guid,
				'metadata_names' => $shortname,
				'limit' => 0,
			));

			if (!empty($current_metadata)) {
				foreach ($current_metadata as $md) {
					$current_metadata_ids[] = $md->id;
				}
			}
		}

		if (empty($current_metadata_ids)) {
			$current_metadata_ids = array();
		}

		$future_metadata = get_input($this->getShortname(), array());

		$params = array(
			'field' => $this,
			'entity' => $entity,
			'metadata_name' => $shortname,
			'value' => $current_metadata,
			'future_value' => $future_metadata,
		);

		// Allow plugins to prevent metadata from being changed
		if (!elgg_trigger_plugin_hook('handle:metadata:before', 'prototyper', $params, true)) {
			return $entity;
		}

		$future_metadata_ids = elgg_extract('id', $future_metadata, array());

		$to_delete = array_diff($current_metadata_ids, $future_metadata_ids);
		if (!empty($to_delete)) {
			elgg_delete_metadata(array(
				'guids' => (int) $entity->guid,
				'metadata_ids' => $to_delete,
				'limit' => 0,
			));
		}

		$keys = array_keys(elgg_extract('name', $future_metadata, array()));

		$ids = array();
		foreach ($keys as $i) {

			$id = $future_metadata['id'][$i];
			$name = $future_metadata['name'][$i];
			$value = $future_metadata['value'][$i];
			$value_type = $this->getValueType();
			$input_type = $this->getType();
			if ($value_type == 'tags' || (!$value_type && $input_type == 'tags')) {
				$value = string_to_tag_array($value);
			}
			$access_id = $future_metadata['access_id'][$i];
			$owner_guid = $future_metadata['owner_guid'][$i];

			if (!is_array($value)) {
				if ($id) {
					update_metadata($id, $name, $value, '', $owner_guid, $access_id);
				} else {
					$id = create_metadata($entity->guid, $name, $value, '', $owner_guid, $access_id, true);
				}
				$ids[] = $id;
			} else {
				if ($id) {
					elgg_delete_metadata_by_id($id);
				}
				foreach ($value as $val) {
					$ids[] = create_metadata($entity->guid, $name, $val, '', $owner_guid, $access_id, true);
				}
			}
		}

		$params = array(
			'field' => $this,
			'entity' => $entity,
			'metadata_name' => $shortname,
			'value' => (count($ids)) ? elgg_get_metadata(array('metadata_ids' => $ids)) : array(),
			'previous_value' => $current_metadata,
		);

		elgg_trigger_plugin_hook('handle:metadata:after', 'prototyper', $params, true);

		return $entity;
	}
public function handle(\ElggEntity $entity) {

		$shortname = $this->getShortname();

		if ($entity->guid) {
			$current_metadata = elgg_get_metadata(array(
				'guids' => (int) $entity->guid,
				'metadata_names' => $shortname,
				'limit' => 0,
			));

			if (!empty($current_metadata)) {
				foreach ($current_metadata as $md) {
					$current_metadata_ids[] = $md->id;
				}
			}
		}

		if (empty($current_metadata_ids)) {
			$current_metadata_ids = array();
		}

		$future_metadata = get_input($this->getShortname(), array());

		$params = array(
			'field' => $this,
			'entity' => $entity,
			'metadata_name' => $shortname,
			'value' => $current_metadata,
			'future_value' => $future_metadata,
		);

		// Allow plugins to prevent metadata from being changed
		if (!elgg_trigger_plugin_hook('handle:metadata:before', 'prototyper', $params, true)) {
			return $entity;
		}

		$future_metadata_ids = elgg_extract('id', $future_metadata, array());

		$to_delete = array_diff($current_metadata_ids, $future_metadata_ids);
		if (!empty($to_delete)) {
			elgg_delete_metadata(array(
				'guids' => (int) $entity->guid,
				'metadata_ids' => $to_delete,
				'limit' => 0,
			));
		}

		$keys = array_keys(elgg_extract('name', $future_metadata, array()));

		$ids = array();
		foreach ($keys as $i) {

			$id = $future_metadata['id'][$i];
			$name = $future_metadata['name'][$i];
			$value = $future_metadata['value'][$i];
			$value_type = $this->getValueType();
			$input_type = $this->getType();
			if ($value_type == 'tags' || (!$value_type && $input_type == 'tags')) {
				$value = string_to_tag_array($value);
			}
			$access_id = $future_metadata['access_id'][$i];
			$owner_guid = $future_metadata['owner_guid'][$i];

			if (!is_array($value)) {
				if ($id) {
					update_metadata($id, $name, $value, '', $owner_guid, $access_id);
				} else {
					$id = create_metadata($entity->guid, $name, $value, '', $owner_guid, $access_id, true);
				}
				$ids[] = $id;
			} else {
				if ($id) {
					elgg_delete_metadata_by_id($id);
				}
				foreach ($value as $val) {
					$ids[] = create_metadata($entity->guid, $name, $val, '', $owner_guid, $access_id, true);
				}
			}
		}

		$params = array(
			'field' => $this,
			'entity' => $entity,
			'metadata_name' => $shortname,
			'value' => (count($ids)) ? elgg_get_metadata(array('metadata_ids' => $ids)) : array(),
			'previous_value' => $current_metadata,
		);

		elgg_trigger_plugin_hook('handle:metadata:after', 'prototyper', $params, true);

		return $entity;
	}
{@inheritdoc}