Writes the custom parent fields

@param string[] $fields
@param ApiInterface $entity

@return void
492235
private function writeCustomParents($fields, ApiInterface $entity) {
        foreach($entity->getCustomParentRelationships() as $customRelationship) {
            $name = $customRelationship->getReferencedFields();
            if(array_key_exists($name, $fields)) {
                $entity->writeAttribute($name, $fields[$name]);
            }
        }
    }
491575
public function fields(EntityInterface $entity)
    {
        $fields = [];
        foreach ((array)$this->getConfig('fields') as $field) {
            if (!$entity->isDirty($field)) {
                continue;
            }
            $fields[$field] = $entity->$field;
            $entity->setDirty($field, false);
        }

        return $fields;
    }
586346
public function decodeItems(EntityInterface $entity) {
		$fields = $this->_getMappedFields();

		foreach ($fields as $map => $field) {
			$val = $entity->get($field);
			if ($this->isEncoded($val)) {
				$entity->set($map, $this->decoded);
			}
		}
	}
259051
private function saveFieldsRecursive(\stdClass $params, ApiInterface $entity, $isCreating, TransactionInterface $transaction) {
        if(!$isCreating) {
            $this->getAcl()->canWrite($entity);
        }

        $isChanged = false;
        $entity->setTransaction($transaction);
        $parents = $this->filterParents($params, $entity);
        $fields = $this->filterFields($params, $entity, $isCreating);


        $this->writeFields($fields, $entity, false);
        $this->writeCustomParents($fields, $entity);
        //Then Update the parents
        /** @var ApiInterface[] $parentEntities */
        $parentEntities = array();
        foreach($parents as $relationship => $subParams) {
            /** @var ApiInterface $parentEntity */
            $parentEntity = $entity->getRelated($relationship);
            if($parentEntity === false) {
                $relation = $entity->getRelationship($relationship);
                $referencedModel = $relation->getReferencedModel();
                $parentEntity = new $referencedModel();
                $subIsCreating = true;
            } else {
                $subIsCreating = false;
            }
            $parentEntities[$relationship] = $parentEntity;
            $parentIsChanged = $this->saveFieldsRecursive($subParams, $parentEntity, $subIsCreating, $transaction);
            if($parentIsChanged) {
                $isChanged = true;
            }
        }
        //Then Check if we want to load from defaults (if creating the specific object)
        if($isCreating) {
            $requiredRelationships = $entity->getDefaultRelationships();
            foreach($requiredRelationships as $alias) {
                //If the entity isn't currently set, set it from the parents
                if($entity->getRelated($alias) === false) {
                    if(!array_key_exists($alias, $parentEntities)) {
                        $message = 'The Required Parent of \'' . $alias . '\' was not found.';
                        $relationship = $entity->getRelationship($alias);
                        $message .= ' Please either create a new \'' . $alias . '\' or set the field \'' . $relationship->getFields() . '\'';
                        throw new Exception($message, 400);
                    }
                    $entity->setRelated($alias, $parentEntities[$alias]);
                }
            }
            $entity->loadDefaults();
        }
        //Then, if loaded from defaults update the fields and other related parents again, to make sure the defaults didn't overwrite them
        foreach($parentEntities as $relationship => $parent) {
            $entity->setRelated($relationship, $parent);
        }
        if($this->writeFields($fields, $entity, true)) {
            $isChanged = true;
        }

        if($isCreating) {
            $entity->fireEventCancel(UploadBase::EVENT_UPLOAD_FILE_CREATE);
            $entity->fireEvent(self::EVENT_DATA_PROPAGATION_CREATE);
        } else {
            $entity->fireEventCancel(UploadBase::EVENT_UPLOAD_FILE_UPDATE);
            $entity->fireEvent(self::EVENT_DATA_PROPAGATION_UPDATE);
        }
        if($isCreating || sizeOf($entity->getChangedFields()) > 0) {
            $isChanged = true;
            $entity->save();
        }
        if($isCreating) {
            //Add the acl in after the save so that the validation can be performed, in case you are looking at a parent, and the parent value is set incorrectly.  This will rollback the transaction anyways
            $this->getAcl()->canWrite($entity);
        }
        $entity->fireEvent(self::EVENT_DATA_FINAL_CLEANUP);
        return $isChanged;
    }
1096037
private function setParent($parentData, AccountInterface $account)
    {
        if (null != $parentData && isset($parentData['id']) && 'null' != $parentData['id'] && '' != $parentData['id']) {
            $parent = $this->getRepository()->findAccountById($parentData['id']);
            if (!$parent) {
                throw new EntityNotFoundException($this->getAccountEntityName(), $parentData['id']);
            }
            $account->setParent($parent);
        } else {
            $account->setParent(null);
        }
    }
1342236
protected function _multiSlug(EntityInterface $entity) {
		$label = $this->getConfig('label');
		$field = current($label);
		$fields = (array)$entity->get($field);

		$locale = [];
		foreach ($fields as $locale => $_) {
			$res = null;
			foreach ($label as $field) {
				$res = $entity->get($field);
				if (is_array($entity->get($field))) {
					$res = $this->generateSlug($field[$locale], $entity);
				}
			}

			$locale[$locale] = $res;
		}
		$entity->set($this->getConfig('slugField'), $locale);
	}
974706
protected function _ensureFields($entity)
    {
        $config = $this->getConfig();
        $fields = [$config['left'], $config['right']];
        $values = array_filter($entity->extract($fields));
        if (count($values) === count($fields)) {
            return;
        }

        $fresh = $this->_table->get($entity->get($this->_getPrimaryKey()), $fields);
        $entity->set($fresh->extract($fields), ['guard' => false]);

        foreach ($fields as $field) {
            $entity->setDirty($field, false);
        }
    }
896639
public function addPropertyValue(&$entity, $field, $value)
    {
        $container = $this->getPropertyValue($entity, $field, $entity);

        if (!is_array($container)) {
            $container = empty($container) ? [] : [$container];
        }
        $container[] = $value;

        $this->setPropertyValue($entity, $field, $container);
    }
363168
public function moveAfter($entity)
    {
        if ($this->parent_id == $entity->parent_id) {
            $this->position = $entity->position + 1;
            $this->save();
        } else {
            $this->moveTo($entity->position + 1, $entity->parent_id);
        }
    }
725580
protected function generateData(EntityInterface $entity) {
    $data = parent::generateData($entity);

    // Find media belonging to node that has the source term, and set its file
    // url in the data array.
    $source_term = $this->utils->getTermForUri($this->configuration['source_term_uri']);
    if (!$source_term) {
      throw new \RuntimeException("Could not locate source term with uri" . $this->configuration['source_term_uri'], 500);
    }

    $source_media = $this->utils->getMediaWithTerm($entity, $source_term);
    if (!$source_media) {
      throw new \RuntimeException("Could not locate source media", 500);
    }

    $source_file = $this->mediaSource->getSourceFile($source_media);
    if (!$source_file) {
      throw new \RuntimeException("Could not locate source file for media {$source_media->id()}", 500);
    }

    $data['source_uri'] = $source_file->url('canonical', ['absolute' => TRUE]);

    // Find the term for the derivative and use it to set the destination url
    // in the data array.
    $derivative_term = $this->utils->getTermForUri($this->configuration['derivative_term_uri']);
    if (!$source_term) {
      throw new \RuntimeException("Could not locate derivative term with uri" . $this->configuration['derivative_term_uri'], 500);
    }

    $route_params = [
      'node' => $entity->id(),
      'media_type' => $this->configuration['destination_media_type'],
      'taxonomy_term' => $derivative_term->id(),
    ];
    $data['destination_uri'] = Url::fromRoute('islandora.media_source_put_to_node', $route_params)
      ->setAbsolute()
      ->toString();

    $token_data = [
      'node' => $entity,
      'media' => $source_media,
      'term' => $derivative_term,
    ];
    $path = $this->token->replace($data['path'], $token_data);
    $data['file_upload_uri'] = $data['scheme'] . '://' . $path;

    // Get rid of some config so we just pass along
    // what the camel route and microservice need.
    unset($data['source_term_uri']);
    unset($data['derivative_term_uri']);
    unset($data['path']);
    unset($data['scheme']);
    unset($data['destination_media_type']);

    return $data;
  }
172912
public function apply(NodeInterface $node, array $context)
    {
        $context['node'] = $node;

        // Check if this template should be applied at all
        if (!$this->isApplicable($context)) {
            return;
        }
        $this->setProperties($node, $context);

        // Create child nodes if applicable
        /** @var Template $childNodeTemplate */
        foreach ($this->childNodes as $childNodeTemplate) {
            $childNodeTemplate->createOrFetchAndApply($node, $context);
        }

        $this->emitNodeTemplateApplied($node, $context, $this->options);
    }
397904
public function update(EntityInterface $entity): void
    {
        $uri = $this->getEntityEndpointUri($entity->id());
        $response = $this->getClient()->put(
            $uri,
            $this->getEntitySerializer()->serialize($entity, 'json')
        );
        $this->getEntitySerializer()->setPropertiesFromResponse($response, $entity);
    }
15226
private function filterParents(\stdClass $params, ApiInterface $entity) {
        $parents = array();
        foreach($params as $key => $value) {
            if(is_object($value) && get_class($value) == 'stdClass') {
                $alias = ucfirst($key);
                if(in_array($alias, $entity->getParentRelationships())) {
                    $parents[$alias] = $value;
                } else {
                    throw new Exception('Could not find the parent field: ' . $key, 400);
                }
            }
        }
        return $parents;
    }
632798
public function setFields( $fields )
	{
		if( is_array( $fields ) && count( $fields ) )
			$this->fields = $fields;
		else
			trigger_error( 'Field Array of Table Definition must no be empty.', E_USER_ERROR );
	}
1310411
protected function assignFields($fields)
    {
        foreach ($fields as $field) {
            if (!$field instanceof FieldInterface) {
                throw new ModelException(sprintf('Field must be an instance of FieldInterface, got "%s"', $this->getType($field)));
            }

            $field->table($this->table);
            $this->fields[$field->name()] = $field;
        }
    }
647631
private void writeObject(JsonGenerator jsonGenerator, Object entity) throws IOException {
        jsonGenerator.writeStartObject();

        writeName(jsonGenerator, entity);
        writeKind(jsonGenerator, entity);
        writeURL(jsonGenerator, entity);

        jsonGenerator.writeEndObject();
    }
1250170
public function setFields (array $fields)
    {
        $this->_fields = array();
        foreach ($fields as $field) {
            $this->append($field);
        }
    }