Drupal 7 theme function outputs word 'array' - drupal-7

In my hook_preprocess_node function I am changing the links by themeing and adding a t() function to allow translation.
The problem is when I render out in my node I get the word "ARRAY" printed out, this is using either
<?php print render($field_downloads); ?> or <?php print $field_downloads); ?>
in my node.
Code in template.php
$list_of_paths = array();
foreach($field_downloads as $index => $data)
{
$file_uri = $data['uri'];
$file_path = file_create_url($file_uri);
$list_of_paths[] = '<strong> >>'. t('DOWNLOAD'). '</strong> '.l(t($data['description']), $file_path);
}
$variables['field_downloads'] .= theme("item_list", array(
'items' => $list_of_paths,
'type' => 'ul',
'attributes' => array('class' => 'downloads'),
));
}

Related

CAKEPHP 4: I cannot upload many files at the same time

Goodnight (or good morning),
I trying to upload multiple files at the same time. I am following the cookbook instructions to build the solution. I always got first file (not and array of files).
Here is my view code...
<?php
/**
* #var \App\View\AppView $this
* #var \App\Model\Entity\Upload $upload
*/
?>
<div class="row">
<aside class="column">
<div class="side-nav">
<h4 class="heading"><?= __('Actions') ?></h4>
<?= $this->Html->link(__('List Uploads'), ['action' => 'index'], ['class' => 'side-nav-item']) ?>
</div>
</aside>
<div class="column-responsive column-80">
<div class="uploads form content">
<?= $this->Form->create($upload, ['type' => 'file']) ?>
<fieldset>
<legend><?= __('Add Upload') ?></legend>
<?php
echo $this->Form->control('name');
echo $this->Form->control('document_type_id', ['options' => $documentTypes]);
echo $this->Form->control('period_id', ['options' => $periods]);
echo $this->Form->control('user_id', ['options' => $users]);
echo $this->Form->control('documents', ['type' => 'file', 'label' => __('Choose PDF Files'), 'accept' => 'application/pdf', 'multiple' => 'multiple']);
?>
</fieldset>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>
</div>
</div>
</div>
And here is my controller code...
public function add()
{
$upload = $this->Uploads->newEmptyEntity();
if ($this->request->is('post')) {
$upload = $this->Uploads->patchEntity($upload, $this->request->getData());
if ($this->Uploads->save($upload)) {
if (!is_dir($this->Parameters->findByName('document_directory')->first()->toArray()['value'] )) {
mkdir($this->Parameters->findByName('document_directory')->first()->toArray()['value'], 0776, true);
}
$documents = $this->request->getData('documents');
$this->loadModel('Dockets');
$dockets = $this->Dockets->findByDocketStateId(1);
$documents_ok = 0;
$documents_nok = 0;
$dockets_without_document = 0;
foreach($documents as $documents_key => $document_to_save)
{
foreach($dockets as $dockets_key => $docket)
{
$contents = file_get_contents($document_to_save);
$pattern = str_replace('-', '', $pattern);
$pattern = str_replace('', '', $pattern);
$pattern = preg_quote($docket->cuit, '/');
$pattern = "/^.*$pattern.*\$/m";
// search, and store all matching occurences in $matches
if(preg_match_all($pattern, $contents, $matches)){
$documentsTable = $this->getTableLocator()->get('Documents');
$document = $documentsTable->newEmptyEntity();
$document->upload_id = $upload->id;
$document->document_type_id = $upload->document_type_id;
$document->period_id = $upload->period_id;
$document->docket_id = $docket->id;
$document->user_id = $this->getRequest()->getAttribute('identity')['id'];
if ($documentsTable->save($document)) {
if (!is_dir($this->Parameters->findByName('document_directory')->first()->toArray()['value'] )) {
mkdir($this->Parameters->findByName('document_directory')->first()->toArray()['value'], 0776, true);
}
$fileobject = $this->request->getData('document');
$destination = $this->Parameters->findByName('document_directory')->first()->toArray()['value'] . 'document_' . $document->id . '.pdf';
// Existing files with the same name will be replaced.
$fileobject->moveTo($destination);
}
$this->Flash->error(__('The document could not be saved. Please, try again.'));
$documents_ok = $documents_ok + 1;
unset($dockets[$dockets_key]);
unset($documents[$documents_key]);
break;
}
}
}
if(!empty($documents)){
//print_r($documents);
$documents_nok = count($documents);
unset($documents);
}
if(!empty($dockets)){
$dockets_without_document = count($dockets);
unset($dockets);
}
$message = __('There were processed ') . $documents_ok . __(' documents succesfully. ') . $documents_nok . __(' documents did not math with a docket. And ') . $dockets_without_document . __(' active dockets ddid not not have a document.');
$this->Flash->success(__('The upload has been saved. ') . $message);
return $this->redirect(['action' => 'view', $upload->id]);
}
$this->Flash->error(__('The upload could not be saved. Please, try again.'));
}
$documentTypes = $this->Uploads->DocumentTypes->find('list', ['keyField' => 'id',
'valueField' => 'document_type',
'limit' => 200]);
$periods = $this->Uploads->Periods->find('list', ['keyField' => 'id',
'valueField' => 'period',
'limit' => 200]);
$users = $this->Uploads->Users->find('list', ['keyField' => 'id',
'valueField' => 'full_name',
'conditions' => ['id' => $this->getRequest()->getAttribute('identity')['id']],
'limit' => 200]);
$this->set(compact('upload', 'documentTypes', 'periods', 'users'));
}
Can you help me to understand what I doing wrong?
Thanks,
Gonzalo
When using PHP, multi-file form inputs must have a name with [] appended, otherwise PHP isn't able to parse out multiple entries, they will all have the same name, and PHP will simply use the last occurrence of that name.
echo $this->Form->control('documents', [
'type' => 'file',
'label' => __('Choose PDF Files'),
'accept' => 'application/pdf',
'multiple' => 'multiple',
'name' => 'documents[]',
]);
Furthermore the following line:
$fileobject = $this->request->getData('document');
should probably be more like this, as there a) is no document field and b) even if there were, you most likely wouldn't want to process the same file over and over again:
$fileobject = $documents[$documents_key];
Also make sure that you have proper validation in place for the uploaded files, even if you don't seem to use the user provided file information, you should still make sure that you've received valid data!

Yii2 using array to build activeform - cannot get & echo second array element

I have created and registered a Yii2 component function 'formSchema' which
contains the array as such:
class FormSchema extends Component{
public function formSchema()
{
$fields = array(
['field' => 'username', 'controltype' => 'textinput'],
['field' => 'email', 'controltype' => 'textArea'],
);
return $fields;
}
}
?>
I call the array in the active form, however I cannot get the
['controltype'] using the same successful method as I do to retrieve ['field'] as
below. I would like to get that array element however seem unable to get any but the first level element:
<div class="usermanager-form">
<?php $form = ActiveForm::begin(['id'=>$model->formName()]); ?>
<?php
$fields = $items = Yii::$app->formschema->formSchema();
foreach($fields as $field)
{
$field = $field['field'];
echo $form->field($model, $field);
}
?>
You may use array values in this way:
$fields = Yii::$app->formschema->formSchema();
foreach ($fields as $field) {
echo $form->field($model, $field['field'])->{$field['controltype']}();
}

Wordpress Loop Tax Query with multiple terms

I have a taxonomy called "fachbereiche". First I load the taxonomies of the current page:
<?php $term_list = wp_get_post_terms($post->ID, 'fachbereiche', array("fields" => "all", 'parent' => '0'));
foreach($term_list as $thisslug)
{
$output = $thisslug->slug;
echo $output;
?>
The current page has the taxonomy slugs: "bauelemente" and "baumarkt". The echo $output returns bauelementebaumarkt.
Now I want to find all posts of a custom post type "marken" with the same taxonomies as we got above ("bauelemente" and "baumarkt"), so I load the following query:
<?php
$loop = new WP_Query(
array(
'post_type' => 'marken',
'post_status'=>'publish',
'posts_per_page'=>-1,
'orderby'=> 'title',
'order'=>'ASC',
'tax_query' => array(
array(
'taxonomy' => 'fachbereiche',
'field' => 'slug',
'terms' => array($output)
),
),
)
);
}
?>
The query returns only the posts with the taxonomy for "baumarkt". I think because the variable $output returns bauelementebaumarkt. I think that you have to seperate "bauelemente" and "baumarkt". Please have in mind that there can be more than 2 terms or just 1.
Your $output should be an array instead of string, so add this before foreach:
$output = array();
Then inside foreach you should do this:
$output[] = $thisslug->slug;
And finally in tax_query it should be like this:
'terms' => $output,

afterFind not working with recursive level 2

Issue with afterFind().
Current code :
<?php
public function afterFind($results, $primary = false)
{
foreach ($results as $key => $val)
{
if (isset($val['User']['country_code']) && isset($val['User']['mobile']))
{
$results[$key]['User']['mobile'] = trim($val['User']['country_code']).trim($val['User']['mobile']);
}
}
return $results;
}
?>
It's working with $this->User->find(), but not working with other models.
I have 3 models. Room, Place and User.
<?php
$this->Place->bindModel(array('belongsTo' => array('User')));
$this->Room->bindModel(array('belongsTo' => array('Place')));
?>
When I try to find Room data :
<?php
$data = $this->Room->find('first');
array(
[Room] => array()
[Place] => array(
[User] => array(
[mobile] => /* here after find not working it should content country code + mobile */
)
)
)
?>
That might be problem with $reset while binding flyby. http://api.cakephp.org/2.1/source-class-Model.html#868-910
Please set $reset to false.
$this->Model->bindModel(array(...),false);

Populating #options, #header for tableselect in ajax callback function

What I am trying to do is display a table with checkboxes on the press of a button by ajax. The table should be initially hidden and get populated on the fly by a function call.
If initially I load $options1 with some dummy values , then after ajax call it throws in an error saying-
Notice: Undefined index: red in theme_tableselect() (line 3285 of
D:\wamp\www\drupal7\includes\form.inc).
where 'red' is the index of a dummy row value and #options don't get populated with the new values. What is the way to get this working ?
Here is the code for the form-
$form['mltag_new']['tag'] = array(
'#type' => 'button',
'#value' => t("Suggest Tags"),
'#ajax' => array(
'callback' => 'mltag_suggest_tags_ajax',
'wrapper' => 'mltag_suggest_tags_table_div',
'effect' => 'slide',
),
);
$options1 = array(); //initial dummy values
$options1['red']['tag'] = "A red row";
$options1['red']['chi'] = "A red row";
$form['mltag_new']['myselector'] = array (
'#type' => 'tableselect',
'#title' => 'My Selector',
'#header' => $header,
'#options' => $options1,
'#prefix' => '<div id="mltag_suggest_tags_table_div">',
'#suffix' => '</div>',
);
return $form;
and the Ajax callback looks something like this-
function mltag_suggest_tags_ajax($form, $form_state) {
//$content has some content
//pass the content to a function
include_once 'includes/content_tag.inc';
$tags = mltag_content_tag($content, variable_get('algo_type'), 20);
if (empty($tags)) {
$output .= t('Content is insufficient to generate Tags using this algorithm. <br>Please choose other algorithm from Settings Page.');
$form['mltag_new']['sample_text']['#markup'] = $output;
return $form['mltag_new']['sample_text'];
}
else {
$algo = variable_get('algo_type');
if ($algo == 1) {
$header = array(
'tag' => t('Tag'),
'frequency' => t('Frequency'),
);
$options = array();
foreach ($tags as $key => $value) {
$options[$key] = array(
'tag' => $key,
'frequency' => $value,
);
}
}
elseif ($algo == 2) {
$header = array(
'tag' => t('Tag'),
'chi' => t('Chi Square Value'),
);
$options = array();
foreach ($tags as $key => $value) {
$options[$key] = array(
'tag' => $key,
'chi' => $value,
);
}
}
$form['mltag_new']['myselector']['#header'] = $header;
$form['mltag_new']['myselector']['#options'] = $options;
return $form['mltag_new']['myselector'];
}
}
I replied to your post on Drupal.org about how I'm working on a somewhat similar problem. Try adding
$form['mltag_new']['myselector'] =
form_process_tableselect($form['mltag_new']['myselector']);
just before your return. Hopefully that helps you more than it did me. Beware that the #options just get rendered when the block reloads from the ajax, but the original $form object doesn't seem to be aware.
I know that this is a few years later, but I found this while searching for my own solution:
The tableselect module creates checkboxes in the $ form that have to be removed. in the example above, they would be in $form['mltag_new']['myselector'] with keys equal to the original $option1 in your original code. If you unset those, then call
$form['mltag_new']['myselector'] = form_process_tableselect($form['mltag_new']['myselector']);
before your return, it will eliminate the dummy rows.

Resources