hypejunction / hypeprototyper
实体原型设计
Requires (Dev)
- composer/installers: 1.*
- hypejunction/hypeapps: >4.0
README
一组用于原型设计和处理实体表单的开发者和管理员工具。此插件试图创建一种简单的方式来构建表单、验证用户输入和显示实体配置文件。
开发者备注
原型
原型有3种不同的外观
- 表单显示一组用于修改实体信息的输入
- 操作验证并处理用户输入
- 配置文件输出实体信息
原型由字段组成,旨在与已注册的Elgg动作相关联。可以使用"prototype",$action
插件钩子填充原型字段。
这里的想法是,相同类型和子类型的实体可以通过多个动作进行修改。例如,用户实体可以通过register
和profile/edit
动作创建和修改。
每个原型都与ElggObject
、ElggUser
或ElggGroup
的一个实例相关联。如果实体尚不存在,则原型创建一个新实例(但仅在表单已提交并验证后保存)。
表单
$user = elgg_get_logged_in_user_entity(); echo hypePrototyper()->form->with($user, 'profile/edit')->view();
$form = hypePrototyper()->form->with(array('type' => 'user'), 'register')->viewBody(); echo elgg_view('input/form', array( 'action' => 'register', 'body' => $body, 'enctype' => 'multipart/form-data', // if we have file inputs ));
操作
$guid = get_input('guid'); $container_guid = get_input('container_guid'); if (!$guid) { $entity = array( 'type' => 'object', 'subtype' => 'blog', 'container_guid' => $container_guid, ); } else { $entity = get_entity($guid); } if (!$entity) { // something is wrong forward(); } hypePrototyper()->action->with($entity, 'blog/edit')->handle();
$entity = array( 'type' => 'object', 'subtype' => 'file', 'access_id' => ACCESS_LOGGED_IN, ); // In case we want to do more stuff with the new entity try { $controller = hypePrototyper()->action->with($entity, 'file/upload'); if ($controller->validate()) { $entity = $controller->update(); } if ($entity) { // do more stuff } } catch (Exception $ex) { // do something with the error }
上述操作将验证表单并将所有值添加到实体中。如果表单验证失败,用户将被转回到表单(表单是粘性的),并解释失败的验证规则。
配置文件
echo hypePrototyper()->profile->with($group, 'groups/edit') ->filter(function($field) { return in_array($field->getShortname(), array('title', 'description', 'tags')); }) ->view($vars);
字段
elgg_register_plugin_hook_handler('prototype', 'profile/edit', 'prepare_profile_edit_form'); function prepare_profile_edit_form($hook, $type, $return, $params) { if (!is_array($return)) { $return = array(); } $entity = elgg_extract('entity', $params); $fields = array( 'name' => array( 'type' => 'text', 'validation_rules' => array( 'max_length' => 50 ) ), 'briefdescription' => 'longtext', 'interests' => array( 'type' => 'tags', 'required' => true, 'show_access' => ACCESS_PRIVATE, ), 'favorite_foods' => array( 'type' => 'text', 'multiple' => true, 'show_access' => true, ), 'eye_color' => array( 'type' => 'dropdown', 'label' => elgg_echo('eye_color'), 'options_values' => array( 'blue' => elgg_echo('blue'), 'brown' => elgg_echo('brown'), ), ), 'looking_for' => array( 'type' => 'checkboxes', 'label' => false, 'help' => false, 'options' => profile_get_looking_for_options(), ), 'height' => array( 'type' => 'text', 'value_type' => 'number', 'multiple' => false, 'show_access' => false, 'required' => true, 'validation_rules' => array( 'min' => 25, 'max' => 50, 'minlength' => 2, 'maxlength' => 4, ), ), 'empathy' => array( 'type' => 'stars', 'data_type' => 'annotation', 'min' => 0, 'max' => 10, ), 'spouse' => array( 'type' => 'autocomplete', 'data_type' => 'relationship', 'value_type' => 'entity', 'inverse_relationship' => false, 'bilateral' => true, 'match_on' => 'friends', ), 'icon' => array( 'data_type' => 'icon', ), ); return array_merge($return, $fields); }
字段定义为$shortname => $value
对,其中$shortname
是属性、元数据、注释等的名称,而$value
是一个描述输入类型(例如文本、下拉列表等)的字符串或包含以下属性的数组
type
- 输入类型,用作elgg_view("input/$type")
(默认text
)data_type
- 用于存储和检索值的模型(默认metadata
)attribute
- 实体属性,例如guidmetadata
- 实体元数据annotation
- 实体注释relationship
- 实体关系icon
- 实体图标category
- 实体类别(hypeCategories)class_name
- 用于实例化具有自定义数据类型的字段的PHP类value_type
- 如果与type
不同,则值的类型,例如当文本输入期望整数时。值类型将自动添加到'type'验证规则中,因此将'value_type' => 'integer'也相当于'value_type' => ['type' => 'integer']input_view
- 用于显示输入的视图,如果不同于"input/$type"output_view
- 用于显示输出的视图,如果不同于"output/$type"required
- 用户输入是否必需(默认false
)admin_only
- 字段是否仅对管理员可见(默认false
)hide_on_profile
- 字段是否应在自动生成的配置文件中隐藏(默认false
)priority
- 字段的顺序(默认500
)show_access
- 是否显示访问输入(默认false
) 这允许用户指定创建的元数据、注释或附件的访问级别label
- 在输入字段旁边显示的标签(默认true
)true
- 设置为elgg_echo("label:$type:$subtype:$shortname")
;false
- 不显示任何标签或任何其他自定义字符串help
- 显示在输入字段旁边的帮助文本(默认true
)true
- 设置为elgg_echo("help:$type:$subtype:$shortname")
;false
- 不显示帮助文本或任何其他自定义字符串multiple
- 用户是否可以复制字段并添加多个值(默认false
)validation_rules
- 规则 => 预期值对的数组。您可以定义自定义验证规则,并使用'validate:$rule','prototyper'
来验证值。有关可用验证器的完整列表,请参阅 hypePrototyperValidators。options
和options_values
- 要传递给输入的选项数组flags
- 以逗号分隔的标志列表,可用于输入/输出过滤- 所有其他选项都将传递给输入视图,因此您可以添加
class
等示例
以下选项适用于 relationship
数据类型
inverse_relationship
- 存储为反向关系bilateral
- 使其成为双边关系(将添加两个关系)
自定义字段
要定义新的字段类型,请按以下方式注册
hypePrototyper()->config->registerType('icon', \hypeJunction\Prototyper\Elements\IconField::CLASSNAME, array( 'accept' => 'image/*', 'value_type' => 'image', 'multiple' => false, 'show_access' => false, 'input_view' => 'input/file', 'output_view' => false, 'ui_sections' => array( 'access' => false, 'multiple' => false, ) ));
以上注册了一个新的输入类型 'icon',其处理类为 IconField,该类扩展了抽象类 Field。第三个参数包含默认键值对(稍后可以在钩子中覆盖)。'ui_sections' 参数指定了在 hypePrototyperUI 提供的管理界面中应禁用哪些部分。