Skip to content
This repository has been archived by the owner on Aug 30, 2019. It is now read-only.

Commit

Permalink
feat(fields): upgrade to new fields API
Browse files Browse the repository at this point in the history
  • Loading branch information
hypeJunction committed Mar 28, 2018
1 parent 0117d25 commit ad09845
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 26 deletions.
39 changes: 13 additions & 26 deletions classes/hypeJunction/Attachments/AddFormField.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,30 @@

namespace hypeJunction\Attachments;

use Elgg\Request;
use Elgg\Hook;
use hypeJunction\Fields\Collection;
use InvalidParameterException;

class AddFormField {

/**
* Add field
*
* @param \Elgg\Hook $hook Hook
* @param Hook $hook Hook
*
* @return \ElggMenuItem[]|null
* @return Collection
* @throws InvalidParameterException
*/
public function __invoke(\Elgg\Hook $hook) {
public function __invoke(Hook $hook) {

$fields = $hook->getValue();
/* @var $fields \hypeJunction\Fields\Collection */

$fields['attachments'] = [
'#type' => 'attachments',
'#section' => 'content',
'#priority' => 700,
'#input' => function(Request $request) {
return elgg_get_uploaded_files('attachments');
},
'#getter' => function(\ElggEntity $entity) {
return hypeapps_get_attachments($entity);
},
'#setter' => function(\ElggEntity $entity) {
return hypeapps_attach_uploaded_files($entity, 'attachments', [
'access_id' => $entity->access_id,
'container_guid' => $entity->guid,
'origin' => 'cms',
]);
},
'#visibility' => function (\ElggEntity $entity) {
return hypeapps_allow_attachments($entity->type, $entity->subtype);
},
'#profile' => false,
];
$fields->add('attachments', new AttachmentsField([
'type' => 'attachments',
'priority' => 700,
'is_profile_field' => false,
]));

return $fields;
}
Expand Down
54 changes: 54 additions & 0 deletions classes/hypeJunction/Attachments/AttachmentsField.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace hypeJunction\Attachments;

use Elgg\Request;
use ElggEntity;
use hypeJunction\Fields\Field;
use Symfony\Component\HttpFoundation\ParameterBag;

class AttachmentsField extends Field {

/**
* {@inheritdoc}
*/
public function raw(Request $request, ElggEntity $entity) {
return elgg_get_uploaded_files($this->name);
}

/**
* {@inheritdoc}
*/
public function validate($value) {
parent::validate($value);
}

/**
* {@inheritdoc}
*/
public function save(ElggEntity $entity, ParameterBag $parameters) {
return hypeapps_attach_uploaded_files($entity, 'attachments', [
'access_id' => $entity->access_id,
'container_guid' => $entity->guid,
'origin' => 'cms',
]);
}

/**
* {@inheritdoc}
*/
public function retrieve(ElggEntity $entity) {
return hypeapps_get_attachments($entity);
}

/**
* {@inheritdoc}
*/
public function isVisible(ElggEntity $entity, $context = null) {
if (!hypeapps_allow_attachments($entity->type, $entity->subtype)) {
return false;
}

return parent::isVisible($entity, $context); // TODO: Change the autogenerated stub
}
}

0 comments on commit ad09845

Please sign in to comment.