hypejunction/hypeprototyperui

Entity prototyping

4.5.0 2015-08-28 18:15 UTC

This package is auto-updated.

Last update: 2024-10-29 04:52:55 UTC


README

Elgg 1.8 Elgg 1.9 Elgg 1.10 Elgg 1.11

User Interface for hypePrototyper

Introduction

The plugin allows developers to present a visual UI for construction forms. This can be useful, when working with volatile entity types that will require admin/user modifications over time.

alt text alt text

Requirements

Even though this plugins runs on 1.8, your theme will need to support jQuery 1.7+.

How to Use

Display the UI

Let's assume you have an action to create/edit new books, e.g. books/save. You may want to differentiate what forms to display, based on what library this book is available in.

Register an action that will take care of storing the form config, e.g. books/prototype/save.

echo elgg_view_form('prototyper/edit', array(
	'action' => 'books/prototype/save',
), array(
	'action' => 'books/save',
	'attributes' => array(
		'type' => 'object',
		'subtype' => 'book',
		'access_id' => ACCESS_PUBLIC,
		'container_guid' => $library_guid,
	),
	'params' => array(
		'layout' => array('main', 'sidebar', 'footer'),
	),
));

Save config

Perhaps the easiest is to store the config as metadata on a library. You can also use plugin settings, annotations or any method that will allow you to retrieve the config to feed into the plugin hook.

In books/prototype/save, save your config:

use hypeJunction\Prototyper\UI\Template;

$container_guid = get_input('container_guid');
$library = get_entity($container_guid);

$prototype = hypePrototyper()->ui->buildPrototypeFromInput();

$library->book_prototype = json_encode($prototype);

Hook into the prototype

elgg_register_plugin_hook_handler('prototype', 'books/save', 'book_form');

function book_form($hook, $type, $return, $params) {

	$book = elgg_extract('entity', $params);
	if (elgg_instanceof($book, 'object', 'book')) {
		$library = $entity->getContainerEntity();
		if ($library->book_prototype) {
			return json_decode($library->book_prototype, true);
		}
	}

	return $return;
}

Display the form

$book = elgg_extract('entity', $vars);
$library = elgg_extract('container', $vars);

$attributes = array(
	'guid' => $book->guid,
	'type' => 'object',
	'subtype' => 'book',
	'container_guid' => $library->guid,
);

$body = hypePrototyper()->form->with($attributes, 'books/save')->viewBody();
$body .= elgg_view('input/submit', array(
	'value' => elgg_echo('save')
));

echo elgg_view('input/form', array(
	'action' => 'action/books/save',
	'body' => $body
));

Handle the form

Now in your action file, all you need to do is use:

$guid = get_input('guid');

$attributes = array(
	'guid' => $guid,
	'type' => 'object',
	'subtype' => 'book',
	'container_guid' => get_input('container_guid'),
);

hypePrototyper()->action->with($attributes, 'books/save')->handle();