itemsProcFunc and selected items in TYPO3 6.2 - typo3

I'm writing an extension which is configured via FlexForms. One element of the FlexForm is of type 'select', with maxitems > 1. I use itemsProcFunc to call an external class method modifying the 'items' array.
This works fine so far, but when I try to save the plugin options in BE, the entries under 'Selected:' vanish. However, the selected values are stored correctly. See below for my flexform configuration .
<settings.flexuserList>
<TCEforms>
<label>Sektionen</label>
<config>
<type>select</type>
<itemsProcFunc>tx_hevpersons_sections->dogetSectionInfo1</itemsProcFunc>
<maxitems>10000</maxitems>
<size>10</size>
</config>
</TCEforms>
</settings.flexuserList>
public function dogetSectionInfo1($params, $conf)
{
print_r($params['row']['pi_flexform']);
$flexform = \TYPO3\CMS\Core\Utility\GeneralUtility::xml2array($params['row']['pi_flexform']);
$grp = $flexform['data']['sDEF']['lDEF']['settings.flexroleList']['vDEF'];
$flexcantonval = $flexform['data']['sDEF']['lDEF']['settings.flexcanton']['vDEF'];
$flexsectionList = $flexform['data']['sDEF']['lDEF']['settings.flexsectionList']['vDEF'];
$flexuserList = $flexform['data']['sDEF']['lDEF']['settings.flexuserList']['vDEF'];
f( strstr( $grp , "|" ) ){
$string = explode(",",$grp);
foreach ($string as $key => $value) {
$array = explode('|',$value);
$nearay[$key] = $array[0];
}
}
if( count($nearay) ){
foreach ($nearay as $key => $value) {
$usergroupFind[$key] = 'FIND_IN_SET("'.$value.'",usergroup)';
}
$string = ' AND '.implode(' OR ', $usergroupFind) . ' ';
}
if( !empty($string) ){
$GLOBALS['TYPO3_DB']->store_lastBuiltQuery = 1;
$res = $GLOBALS['TYPO3_DB']->exec_SELECTquery("uid, name ", 'fe_users', 'deleted=0 AND disable=0 '.$string );
while($entry = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res))
{
$params['items'][count($params['items'])] = array(trim($entry['name'] ), $entry['uid']);
}
$GLOBALS['TYPO3_DB']->sql_free_result($res);
}
return $params;
}
When a printed flexfrom , I got 2 xml records in which one has comma seperated values stored inside the xml while the other has empty values . Can some one help me with this ?

I think you should return config instead of params.
One of my working example, might be your helps:
TYPO3 v7.6.2
-FlexForm settings
<settings.eventID>
<TCEforms>
<label>Available Event</label>
<config>
<type>select</type>
<size>1</size>
<minitems>0</minitems>
<maxitems>1</maxitems>
<itemsProcFunc>VENDOR\EXT\Controller\ControllerName->flexFormsEventsListItems</itemsProcFunc>
<items type="array"></items>
</config>
</TCEforms>
</settings.eventID>
-Action
public function flexFormsEventsListItems($config){
$formsRepository = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows('fields',
'table_name',
'WHERE clause', '', '', '', '');
$formsData = array(
'Events' => $formsRepository,
);
// create option list
$optionList = array();
foreach($formsRepository as $key=>$item){
$label = $item['title'];
$value = $item['uid'];
$optionList[] = array(0 => $label, 1 => $value);
}
// return config
$config['items'] = array_merge($config['items'], $optionList);
return $config;
}

Related

Perl: unable to extract sibling value using Twig::XPath syntax

Recently I start to use XML::Twig::XPath but the module does not seem to recognize an xpath syntax.
In the following XML, I want the value of "Txt" node if the value of PlcAndNm node is "ext_1"
<?xml version="1.0" encoding="UTF-8"?>
<root>
<Document>
<RedOrdrV03>
<MsgId>
<Id>1</Id>
</MsgId>
<Xtnsn>
<PlcAndNm>ext_1</PlcAndNm>
<Txt>1234</Txt>
</Xtnsn>
<Xtnsn>
<PlcAndNm>ext_2</PlcAndNm>
<Txt>ABC</Txt>
</Xtnsn>
</RedOrdrV03>
</Document>
<Document>
<RedOrdrV03>
<MsgId>
<Id>2</Id>
</MsgId>
<Xtnsn>
<PlcAndNm>ext_1</PlcAndNm>
<Txt>9876</Txt>
</Xtnsn>
<Xtnsn>
<PlcAndNm>ext_2</PlcAndNm>
<Txt>DEF</Txt>
</Xtnsn>
</RedOrdrV03>
</Document>
</root>
I have tried whit expression //Xtnsn[PlcAndNm="ext_1"]/Txt but I received an error
This is the code:
use XML::Twig::XPath;
my $subelt_count = 1;
my #processed_elements;
my $xmlfile = 'c:/test_file.xml';
my $parser = XML::Twig->new(
twig_roots => { 'RedOrdrV03' => \&process_xml } ,
end_tag_handlers => { 'Document' },
);
$parser->parsefile($xmlfile);
sub process_xml {
my ( $twig, $elt ) = #_;
push( #processed_elements, $elt );
if ( #processed_elements >= $subelt_count ) {
my $MsgId = $twig->findvalue('RedOrdrV03/MsgId/Id');
my $Xtnsn_Txt1 = $twig->findvalue('//Xtnsn[PlcAndNm="ext_1"]/Txt');
print "MsgId: $MsgId - Xtnsn_Txt1: $Xtnsn_Txt1\n";
}
$_->delete for #processed_elements;
#processed_elements = ();
$twig->purge;
}
Is there a simple way of using xpath to obtain the value?
I know that a possibility is somenthing like:
my $Xtnsn_Txt1 = $twig->first_elt( sub { $_[0]->tag eq 'PlcAndNm' && $_[0]->text eq 'ext_1' })->next_sibling()->text();
but I prefer using the simplest XPath syntax,
Thanks in advance for your help!
You can use this:
my $Xtnsn_Txt1 = $twig->findvalue('//Xtnsn/PlcAndNm[string()="ext_1"]/../Txt');
Another approach could be :
//Txt[preceding-sibling::PlcAndNm[.="ext_1"]]
You can also modify a little bit your XPath expression to see if it works with :
//Xtnsn[./PlcAndNm[contains(.,"ext_1")]]/Txt
EDIT : This works fine with the original XML::XPath module :
use XML::XPath;
use XML::XPath::Node::Element;
my $xp = XML::XPath->new(filename => 'pathtoyour.xml');
my $nodeset = $xp->find('//Xtnsn[PlcAndNm="ext_1"]/Txt');
foreach my $node ($nodeset->get_nodelist) {
print XML::XPath::Node::Element::string_value($node),"\n\n";
}
Output : 1234 9876

How do I attach a pdf file to a Gravity Forms Notification?

Gravity forms offers a way to attach files from the file uploader (See code below), but how would I change this code to simply attach my own PDF file from either a hidden field value or simply paste the pdf file within this code? I tried a few things but it didn't work. Any help would be appreciated!
add_filter( 'gform_notification', 'change_user_notification_attachments', 10, 3 );
function change_user_notification_attachments( $notification, $form, $entry ) {
//There is no concept of user notifications anymore, so we will need to target notifications based on other criteria, such as name
if ( $notification['name'] == 'User Notification' ) {
$fileupload_fields = GFCommon::get_fields_by_type( $form, array( 'fileupload' ) );
if(!is_array($fileupload_fields))
return $notification;
$attachments = array();
$upload_root = RGFormsModel::get_upload_root();
foreach( $fileupload_fields as $field ) {
$url = $entry[ $field['id'] ];
$attachment = preg_replace( '|^(.*?)/gravity_forms/|', $upload_root, $url );
if ( $attachment ) {
$attachments[] = $attachment;
}
}
$notification['attachments'] = $attachments;
}
return $notification;
}
Based on that code, something like this should work. Replace the $url value with the URL to your PDF.
add_filter( 'gform_notification', 'change_user_notification_attachments', 10, 3 );
function change_user_notification_attachments( $notification, $form, $entry ) {
if ( $notification['name'] == 'User Notification' ) {
$url = 'http://yoursite.com/path/to/file.pdf';
$notification['attachments'][] = $url;
}
return $notification;
}

Showing course image on custom page in Moodle

I have created a custom page on which all available courses are displayed. I have also uploaded the image for the course and now want to show the name of the course along with the image. I am able to get the names of courses from the database but how to get the image.
Try something like this
// Create a course_in_list object to use the get_course_overviewfiles() method.
require_once($CFG->libdir . '/coursecatlib.php');
$course = new course_in_list($courseid);
$outputimage = '';
foreach ($course->get_course_overviewfiles() as $file) {
if ($file->is_valid_image()) {
$imagepath = '/' . $file->get_contextid() .
'/' . $file->get_component() .
'/' . $file->get_filearea() .
$file->get_filepath() .
$file->get_filename();
$imageurl = file_encode_url($CFG->wwwroot . '/pluginfile.php', $imagepath,
false);
$outputimage = html_writer::tag('div',
html_writer::empty_tag('img', array('src' => $imageurl)),
array('class' => 'courseimage'));
// Use the first image found.
break;
}
}
echo $outputimage;
You may get course image directly from 'overviewfiles' files area.
function get_course_image()
{
global $COURSE;
$url = '';
require_once( $CFG->libdir . '/filelib.php' );
$context = context_course::instance( $COURSE->id );
$fs = get_file_storage();
$files = $fs->get_area_files( $context->id, 'course', 'overviewfiles', 0 );
foreach ( $files as $f )
{
if ( $f->is_valid_image() )
{
$url = moodle_url::make_pluginfile_url( $f->get_contextid(), $f->get_component(), $f->get_filearea(), null, $f->get_filepath(), $f->get_filename(), false );
}
}
return $url;
}

Getting a menu delivered via REST

I am trying to get a menu via REST and I've created a new module and rest resource plugin that allows for GET on /entity/restmenu/{menu_name}.
I can successfully return this example json using this function when I hit the URL.
public function get(EntityInterface $entity) {
$result = array();
for ($i = 0; $i < 10; $i++) {
$temp = array(
'title' => 'Test ' . $i,
'href' => '#/' . $i
);
array_push($result, $temp);
}
return new ResourceResponse(json_encode($result));
}
I cannot figure out how to load the menu based on $entity. If I hit my URL (http://dang.dev:8888/entity/restmenu/main?_format=hal_json) $entity's value is 'main' which is the machine name of the main menu.
I've tried using Drupal menu tree, but I am not having luck, and debugging this thing with only JSON responses is quite difficult.
How do I get menu item titles and paths based on the menu machine name?
EDIT
Ok, sort of figured it out.
public function get($entity) {
$menu_name = $entity;
$menu_parameters = \Drupal::menuTree()->getCurrentRouteMenuTreeParameters($menu_name);
$tree = \Drupal::menuTree()->load($menu_name, $menu_parameters);
$renderable = \Drupal::menuTree()->build($tree);
$result = array();
foreach (end($renderable) as $key => $val) {
$temp = array(
'menu_item' => $val,
'route' => $key
);
array_push($result, $temp);
}
return new ResourceResponse(json_encode($result));
}
Right now that will output:
[
{
"menu_item":{
"is_expanded":false,
"is_collapsed":false,
"in_active_trail":false,
"attributes":"",
"title":"Home",
"url":{
},
"below":[
],
"original_link":{
}
},
"route":"standard.front_page"
},
{
"menu_item":{
"is_expanded":false,
"is_collapsed":false,
"in_active_trail":false,
"attributes":"",
"title":"Communities",
"url":{
},
"below":[
],
"original_link":{
}
},
"route":"menu_link_content:139d0413-dc50-4772-8200-bc6c92571fa7"
}
]
any idea why url or original_link are empty?
This was the correct answer:
public function get($entity) {
$menu_name = $entity;
$menu_parameters = \Drupal::menuTree()->getCurrentRouteMenuTreeParameters($menu_name);
$tree = \Drupal::menuTree()->load($menu_name, $menu_parameters);
$result = array();
foreach ($tree as $element) {
$link = $element->link;
array_push($result, array(
'title' => $link->getTitle(),
'url' => $link->getUrlObject()->getInternalPath(),
'weight' => $link->getWeight()
)
);
}
return new ResourceResponse(json_encode($result));
}

access sub child value by libxml::xpathcontext

I want to access the value of sub child and modify it. This is my xml
<config xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0">
<outer1 xmlns="http://blablabla" >
<inner>
<name>
<prenom>Hello</prenom>
</name>
<profession>warrior</profession>
</inner>
<inner>
<name>
<prenom>Hello</prenom>
</name>
<org>wwf</org>
<profession>warrior</profession>
</inner>
</outer1>
and this is my code
my $dom = XML::LibXML->load_xml( location => $xml);
my $context = XML::LibXML::XPathContext->new( $dom->documentElement() );
$context->registerNs( 'u' => '"urn:ietf:params:xml:ns:netconf:base:1.0' );
$context->registerNs( 'u' => 'http://blablabla');
for my $node ($context->findnodes('//u:inner') ) {
for my $node2 ($node->findnodes('//u:name') ) {
#if (($node->findnodes('u:name', $node2) ->size) != 1) {next;}
my ($mh) = $node->findnodes('u:prenom', $node2);
my $size = $node->findnodes('u:prenom', $node2) ->size;
print "size $size";
if ($size != 1) {next;}
$mh ->removeChildNodes();
$mh->appendText('World12456');
print "mh = $mh";
}
}
I want to access prenom and modify it to 'World12456'. With currrent code; I got this error XPath error : Undefined namespace prefix
error : xmlXPathCompiledEval: evaluation failed. Then I tried different way
for my $node ($context->findnodes('//u:inner') ) {
my ($mh) = $context->findnodes('u:name/prenom', $node);
my $size = $context->findnodes('u:name/prenom', $node) ->size;
print "size $size";
if ($size != 1) {next;}
$mh ->removeChildNodes();
$mh->appendText('World12456');
print "mh = $mh";
}
Then I get the size is 0 for both. It doesn't find the tag prenom. With
for my $node ($context->findnodes('//u:inner/name')
It displays nothing.
I am sorry if this is duplicate but I don't find any link to access the sub child with xpathcontext yet.
I got it . I just need to put u for each element
for my $node ($context->findnodes('//u:inner/u:name')