Modify admin configuration values on save - php

I have created a configuration form in Grav's admin panel, and I want to extend/modify some of it's values on save.
More precisely, I have a list form element that looks like this in the blueprint:
topics:
type: list
fields:
.name:
type: text
.unique_id:
type: text
readonly: true
default: generate_on_save
On save, I want to replace all generate_on_save values with a unique id.
I tried to hook into the onAdminSave Event, but the Event Object contained just an instance of \Grav\Common\Data\Blueprint and no actual form data. I then tried to modify the request object, but when I register the modified request in the grav container, I get an error Cannot override frozen service 'request'.
How can I accomplish this task?

I did the following which works fine:
In config file /user/themes/quark/blueprints.yaml, I copied your field definition.
In Admin I added some topics on the config page of theme Quark.
The 'Save' action was captured by the following eventhandler:
public function onAdminSave(Event $event) {
/** #var Data */
$form = $event['object'];
$topics = $form['topics'];
foreach ($topics as &$topic) {
if ($topic['unique_id'] === 'generate_on_save') {
$topic['unique_id'] = str_rot13($topic['name']);
}
}
// Note: Updated $form['topics'] must be re-assigned
$form['topics'] = $topics;
}
The topics with there "unique" values have correctly been written to /user/config/themes/quark.yaml

Related

How to save an ID of the post to metadata?

I am using a Toolset plugin for Wordpress and try to make it save the id of the page where the user fills up the form.
This plugin has "hooks" for it.
This is what they say about it.
cred_save_data
Description
This hook allows doing a custom action when post data is saved to the database.
Arguments
post_id. The id of the current created or edited post.
form_data. An associative array of data about current form:
id. The form of ID.
post_type. The post type that the form operates on.
form_type. The type of form - 'new' or 'edit'.
container_id. Refers to the post_id of the post, page or custom post type that contains the CRED form
EXAMPLE:
add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data)
{
// if a specific form
if ($form_data['id']==12)
{
if (isset($_POST['my_custom_field']))
{
// add it to saved post meta
add_post_meta($post_id, '__my_custom_field', $_POST['my_custom_field'], true);
}
}
}
so I added this modified PHP code in functions.php of the theme:
add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data)
{
// if a specific form
if ($form_data['id']==2080) // the id of the form
{
if (isset($_POST['container_id']))
{
// add it to saved post meta
add_post_meta($post_id, 'formetadata', $_POST['container_id'], true);
}
}
}
where for metadata is a slug for custom field created to collect metadata, including the id of the page, where the form was filled up.
It does not work. The form collects data, creates a post, but the field for metadata remains empty.
What did I do wrong?
I don't have enough knowledge about PHP and may be completely misunderstood instructions...
So looking at the docs and your function I see they use the key container_id and you use $_POST['container_id']. Im just guessing but it sounds like those are the same id? If so then you can access it via the $form_data like below:
add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data)
{
// if a specific form
if ($form_data['id']==2080) // the id of the form
{
if (isset($_POST['container_id']))
{
// add it to saved post meta
add_post_meta($post_id, 'formetadata', $form_data['container_id'], true);
}
}
}

How to create a Web hook publish action for custom post types in Wordpress using Hook press plugin

I need to trigger a "publish_post" action for a custom post type. I defined a trigger named "publish_book" under "hook press_action".
$hookpress_actions = array('publish_book'=>array('BOOK'),'add_attachment'=>array('ATTACHMENT'),
........
);
After that I defined the field names I need to pass in "hookpress_get_fields" function as follows.
if ($type == 'BOOK') $fields = array('post_url','post_type');
I can see the action and the fields listed in the web hook settings page. It even triggers when a new book post is published. However, the post_type, post_title fields values are not sent in the request. How can I capture the values of those fields and pass them to the web hook URL ?
After going through the plugin code I noticed that we need to explicitly add the fields to the request. We need to add following changes to the "hookpress_generic_action" function (defined in Include.php file) which is called during a publish event.
foreach($args as $i => $arg) {
$newobj = array();
switch($arg_names[$i]) {
case 'BOOK':
$newobj = get_post($arg,ARRAY_A);
break;
default:
$newobj[$arg_names[$i]] = $arg;
}
}

How to display value option from dropdown list in Elgg

Elgg is build on the MVC framework. My main agenda is to be able to save the value selected from the dropdown list, after which is to then display the chosen value the item listing. The following code is actually constructed in PHP that is following the Elgg framework closely.
What I have managed to do is to make use of the existing Elgg framework to display the dropdown list. In which, the dropdown list is created by the creation of a form in the following directory: mod/plugin/views/default/forms/plugin/form.php. I have hence made use of the existing Elgg framework (input/dropdown)to create my dropdown list as a form.
Secondly, I have managed to save the values chosen in the dropdown list and display the value in a success message. This is done in the action directory which will allow the values to be saved into the database when user clicks on 'save' button.
Code for saving and displaying value:
<?php
/**
* Elgg options uploader/submit action
*
* #package ElggFile
*/
// get the input variables
$list = get_input('OptionItems');
$container_guid = (int) get_input('container_guid', 0);
if ($container_guid == 0)
{
$container_guid = elgg_get_logged_in_user_guid();
}
$my_select_guid = (int) get_input (file_guid);
//create a new my_select object
$my_select = new ElggObject();
$my_select -> dropdown = $list;
$my_select ->container_guid = $container_guid;
//save to database and get id of the new my_blog
$my_select_guid = $my_select->save();
if($my_select_guid){
system_message("Your action post = " . $list);
//to add new muy_select object to river
add_to_river('river/object/file/create', 'create', elgg_get_logged_in_user_guid(), $list->guid);
}
else{
register_error("Your action post is not saved");
}
However, at this point, I am stuck in displaying the chosen value of the dropdown list as an extended view, within the view/default/object/file/
How am I able to do this?
It's all in Elgg documentation about the views that I linked for you before: http://learn.elgg.org/en/1.12/guides/views.html#extending-views

joomla 3.x tags in custom component fails

Running Joomla 3.3.0-dev
I'm following the info posted here about adding tag support to a third-party component.
I've added the content type to the #__content_types table and modified my table file like this:
class MycomponentTableElement extends JTable
{
public $tagsHelper = null; // failed when protected and public
public function __construct(&$_db)
{
parent::__construct('#__mycomponent', 'id', $_db);
// Add Joomla tags
JObserverMapper::addObserverClassToClass('JTableObserverTags', 'MycomponentTableElement', array('typeAlias' => 'com_mycomponent.element'));
//$this->_observers = new JObserverUpdater($this); JObserverMapper::attachAllObservers($this); // failed with or without this line
}
I added the tag field in the edit template, and it worked fine-- but when I save an object I get the following error:
Save failed with the following error: Unknown column 'tagsHelper' in 'field list'
What am I missing? There's no other steps (besides front-end steps!) that are mentioned. It seems like I need to modify the model but that info is not applicable.
Thanks
"This Page Needs Copy Editing" and it's really true!
I also follow initial steps as described in page
Register a 'Content type' for the extension view(s)
Add 'Observer methods' to the extension table class(es)
Add 'Tag fields' to the extension edit forms
But to make field tag works on custom extensions I need to explicit set form field value in view file of backend:
$tagsHelper = new JHelperTags;
$this->form= $this->get('Form');
$this->form->setValue('tags', null, $tagsHelper->getTagIds( $this->item->id, 'com_custom.viewname') );
in this way on edit page all seems to work correctly.. surely exist better and more clean method, but until doc page will not be updated, this can help someone!
1- Add tag field to your xml form file or edit template file
2- Modify #__content_types table file:
function __construct(&$db)
{
parent::__construct('#__ir_products', 'id', $db);
JTableObserverTags::createObserver($this, array('typeAlias' => 'com_itemreview.product'));
}
3- Modify model file getItem function:
public function getItem($pk = null)
{
$item = parent::getItem($pk);
if (!empty($item->id))
{
$item->tags = new JHelperTags;
$item->tags->getTagIds($item->id, 'com_yourcomponent.yourmodel');
}
return $item;
}

Symfony: embedding collection of forms

I'm trying to embed collection of forms into one based on this cookbook: http://symfony.com/doc/current/cookbook/form/form_collections.html
I have following database shcema: product ---< POS shortcut >--- POS item
in controller i'm initiatig form like this:
$pos = new PosList();
for ($i=0;$i<10 ;$i++ ) {
$scut1 = new PosShortcut();
$pos->addPosShortcut($scut1);
}
$form = $this->createForm(new PosListType(), $pos);
Then i have form for POSShortcut:
$builder
->add('posList','hidden')
->add('product','entity',array(
'required'=>false,
'empty_value'=>'Choose product...',
'label'=>'Shordcut product',
'class'=>'StockBundle:Product',
'property'=>'name'
));
And of course main form, what includes PosShortcut form:
<...>
->add('posShortcut','collection',array('type'=>new PosShortcutType()))
<...>
Now the problem is that for a POS shortcut item, POSList id is set to NULL when it's written in database. Reason is very simple, i haven't set PosList value for new shortcut when i'm creating them in controller at firstplace. But the issue is that i can not do that in this state, because POS item does not exists in database yet.
How to get over this problem ?
class PosList
{
public function addPosShortcut($shortcut)
{
$this->shortcuts->add($shortcut);
$shortcut->setPosList($this); // *** This will link the objects ***
}
Doctrine 2 will take care of the rest. You should be aware that all 10 of your shortcuts will always be posted.

Categories