在OctoberCMS中,我想简单地通过附加不同的插件组件来更改页面进程。
我有一个插件组件(makeform
),它插入一个表单(在数据库表中定义)。
单击表单的submit
按钮将调用调用process()
的onSubmit()
。
process()
是在另一个插件组件中定义的函数。
我可以在不知道其他插件或其组件的名称的情况下从process()
内部调用makeform
吗?也就是说,不需要use 'Acme\Plugin\Components\ProcessForm', 'processForm';
(二)或者,我是否可以编程地发现另一个附加组件的名称和它的插件,然后以某种方式指定它是process()
函数?
或者是使用静态properties
下拉列表来选择哪个进程,然后动态地添加组件。总是假设我可以超越$this->addComponent('Acme\Plugin\Components\ProcessForm', 'processForm');
init()
。
编辑:试验
我希望动态的addComponent()
。
无论我在哪里放置它,无论是在init()
中还是在其他地方,我都会得到错误:
未为组件"Acme\Plugin\Components\ProcessForm“注册类名。检查组件插件。
即使我没有使用其中的一个类。
许多网上引用了这条错误信息,但对我没有帮助。
编辑:进一步解释
(希望)对我正在努力实现的目标作一个简化的解释。
本质上,我设想一个由一串组件组成的页面过程。
每个组件在下一个组件中调用一个函数,直到进程结束。
可以简单地通过替换组件来修改整个过程。
我猜想连接组件的唯一方法是标准化函数名。所以这个(可能?)要求组件属于流程的特定阶段,但如果每个组件都能在任何阶段(在适当的情况下)适合,则是理想的。
发布于 2017-10-27 02:12:41
我认为最好的方法是定义另一个属性,在其中设置插件的命名空间。
public function defineProperties(){
'pluginName' => [
'label' => 'Plugin Namespace to call process method from',
'type' => 'text'
]
}
--
public function onSubmit(){
$plugin = $this->property('pluginName');
$plugin::process($formData);
}
通过这种方式,您可以保持组件逻辑不受任何硬编码插件名称的影响。
编辑: 30/10/17
我不确定是否有办法列出应用程序中的所有可用组件。另一种方法是设置一个Settings
页面,其中包含一个repeater
,在该页面中,可以使用名称空间声明所有可用的组件。
您可以将其解析为onSubmit
方法中的数组,并将其返回给下拉列表。
public function defineProperties(){
'components' => [
'label' => 'Plugin Namespace to call process method from',
'type' => 'dropdown',
'options' => 'getComponentsOptions' // optional but easier to understand
]
}
public function getComponentsOptions(){
$components = Settings::get('components');
$options = [];
foreach ($components as $component)
{
$options[$component['namespace']] = $component['name'];
}
return $options;
}
/模型/设置/字段s.yaml
fields:
components:
type: repeater
form:
fields:
name:
placeholder: My Component Name
span: left
namespace:
placeholder: Acme\Name\Components\MyComponent;
span: right
/Models/Settings.php
class Settings extends Model
{
public $implement = ['System.Behaviors.SettingsModel'];
// A unique code
public $settingsCode = 'acme_name_settings';
// Reference to field configuration
public $settingsFields = 'fields.yaml';
}
发布于 2017-10-28 19:18:26
https://stackoverflow.com/questions/46966374
复制