classes/hypeJunction/Prototyper/Elements/AnnotationField.php
public function handle(\ElggEntity $entity) {

		$shortname = $this->getShortname();

		$current_annotations = elgg_get_annotations(array(
			'guids' => (int) $entity->guid,
			'annotation_names' => $shortname,
		));

		if (is_array($current_annotations) && count($current_annotations)) {
			foreach ($current_annotations as $ann) {
				$current_annotations_ids[] = $ann->id;
			}
		}

		if (!is_array($current_annotations_ids)) {
			$current_annotations_ids = array();
		}

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

		$params = array(
			'field' => $this,
			'entity' => $entity,
			'annotation_name' => $shortname,
			'value' => $current_annotations,
			'future_value' => $future_annotations,
		);

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

		$future_annotations_ids = elgg_extract('id', $future_annotations, array());

		$to_delete = array_diff($current_annotations_ids, $future_annotations_ids);
		foreach ($to_delete as $id) {
			elgg_delete_annotation_by_id($id);
		}

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

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

			$id = $future_annotations['id'][$i];
			$name = $future_annotations['name'][$i];
			$value = $future_annotations['value'][$i];
			if ($this->getValueType() == 'tags') {
				$value = string_to_tag_array($value);
			}
			$access_id = $future_annotations['access_id'][$i];
			$owner_guid = $future_annotations['owner_guid'][$i];

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

		$params = array(
			'field' => $this,
			'entity' => $entity,
			'annotation_name' => $shortname,
			'value' => (count($ids)) ? elgg_get_annotations(array('annotation_ids' => $ids)) : array(),
			'previous_value' => $current_annotations,
		);

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

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

		$shortname = $this->getShortname();

		$current_annotations = elgg_get_annotations(array(
			'guids' => (int) $entity->guid,
			'annotation_names' => $shortname,
		));

		if (is_array($current_annotations) && count($current_annotations)) {
			foreach ($current_annotations as $ann) {
				$current_annotations_ids[] = $ann->id;
			}
		}

		if (!is_array($current_annotations_ids)) {
			$current_annotations_ids = array();
		}

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

		$params = array(
			'field' => $this,
			'entity' => $entity,
			'annotation_name' => $shortname,
			'value' => $current_annotations,
			'future_value' => $future_annotations,
		);

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

		$future_annotations_ids = elgg_extract('id', $future_annotations, array());

		$to_delete = array_diff($current_annotations_ids, $future_annotations_ids);
		foreach ($to_delete as $id) {
			elgg_delete_annotation_by_id($id);
		}

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

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

			$id = $future_annotations['id'][$i];
			$name = $future_annotations['name'][$i];
			$value = $future_annotations['value'][$i];
			if ($this->getValueType() == 'tags') {
				$value = string_to_tag_array($value);
			}
			$access_id = $future_annotations['access_id'][$i];
			$owner_guid = $future_annotations['owner_guid'][$i];

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

		$params = array(
			'field' => $this,
			'entity' => $entity,
			'annotation_name' => $shortname,
			'value' => (count($ids)) ? elgg_get_annotations(array('annotation_ids' => $ids)) : array(),
			'previous_value' => $current_annotations,
		);

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

		return $entity;
	}
{@inheritdoc}