I have block in my module. I loading article type "video" and one field is "long string". Content is Media - remote video.
$body = $articles->get('body')->getValue();
$shortBody = $articles->get('field_video')->value;
$items[] = array(
'title' => $articles->getTitle(),
'text' => $body[0]['value'],
'id' => $articles->id(),
'path' => $alias,
'date' => $articles->get('revision_timestamp')->value,
'shorttext' => $shortBody
);
In key shorttext I have string <drupal-media data-align="center" data-entity-type="media" data-entity-uuid .... </drupal-media>. Template see this as string, so it dont render video file. How can I render video file from this string in Twig template?
Thanks
OK, my solution was this. I change field type to Media - entity.
Then I rewrite $shortBody = $articles->get('field_video')->value; to $shortBody = $articles->get('field_video')->getValue();
and in Twig file
{{ drupal_entity('media', item.shorttext) }}
This is working nicely.
Related
I am using Krajee mdpf tool(http://demos.krajee.com/mpdf) on Yii 2. I tried to generate a view and I succeed with the render() function. The problem is that it displays a layout. So I decided to use renderPartial() function to only display the htlm of the view but I get this error:
This page contains the following errors:
error on line 1 at column 1: Document is empty
error on line 1 at column 1: Encoding error
Below is a rendering of the page up to the first error.
Here is my actionPdfTest function:
public function actionPdfTest()
{
// get your HTML raw content without any layouts or scripts
$content = Yii::$app->controller->renderPartial('_example');
// setup kartik\mpdf\Pdf component
$pdf = new Pdf([
// set to use core fonts only
'mode' => Pdf::MODE_CORE,
// A4 paper format
'format' => Pdf::FORMAT_A4,
// portrait orientation
'orientation' => Pdf::ORIENT_PORTRAIT,
// stream to browser inline
'destination' => Pdf::DEST_BROWSER,
// your html content input
'content' => $content,
// format content from your own css file if needed or use the
// enhanced bootstrap css built by Krajee for mPDF formatting
'cssFile' => '#vendor/kartik-v/yii2-mpdf/src/assets/kv-mpdf-bootstrap.min.css',
// any css to be embedded if required
'cssInline' => '.kv-heading-1{font-size:18px}',
// set mPDF properties on the fly
'options' => ['title' => 'Krajee Report Title'],
// call mPDF methods on the fly
'methods' => [
'SetHeader'=>['Krajee Report Header'],
'SetFooter'=>['Krajee Report Footer'],
]
]);
// return the pdf output as per the destination setting
return $pdf->render();
}
Code of _example view:
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
/* #var $this yii\web\View */
/* #var $form yii\widgets\ActiveForm */
/* #var $model app\models\LoginForm */
$this->title = 'Hello World';
?>
<h1><?= Html::encode($this->title) ?></h1>
Please Can you help me?
Thank you every body,
I found the solution. It was to add :
Yii::$app->response->format = \yii\web\Response::FORMAT_RAW;
before the:
$content = Yii::$app->controller->renderPartial('_example');
Here is the final code:
public function actionPdfTest()
{
Yii::$app->response->format = \yii\web\Response::FORMAT_RAW;
// get your HTML raw content without any layouts or scripts
$content = Yii::$app->controller->renderPartial('_example');
// setup kartik\mpdf\Pdf component
$pdf = new Pdf([
// set to use core fonts only
'mode' => Pdf::MODE_CORE,
// A4 paper format
'format' => Pdf::FORMAT_A4,
// portrait orientation
'orientation' => Pdf::ORIENT_PORTRAIT,
// stream to browser inline
'destination' => Pdf::DEST_BROWSER,
// your html content input
'content' => $content,
// format content from your own css file if needed or use the
// enhanced bootstrap css built by Krajee for mPDF formatting
'cssFile' => '#vendor/kartik-v/yii2-mpdf/src/assets/kv-mpdf-bootstrap.min.css',
// any css to be embedded if required
'cssInline' => '.kv-heading-1{font-size:18px}',
// set mPDF properties on the fly
'options' => ['title' => 'Krajee Report Title'],
// call mPDF methods on the fly
/*
'methods' => [
'SetHeader'=>['Krajee Report Header'],
'SetFooter'=>['Krajee Report Footer'],
]
*/
]);
// return the pdf output as per the destination setting
return $pdf->render();
}
this error Message mostly appears, if there is no correct handling of http-header, document-format and/or sending output to the browser.
I don't know, what return $pdf->render(); exactly is doing. But I guess your problem is a mix of sending wrong header, sending header multiple times and/or sending output that doesn't matches the header.
So you should think about:
What output-type you expect for your script? Check, if you set the http-Header that matched your output type.
Check if $pdf->render(); outputs something more than the return value.
If $pdf->render() returns an pdf, you should send this as file to the browser, like that ("pseudo-code"). Using sendFile, sendContentAsFile or sendStreamAsFile depends on the output of $pdf->render() ( https://www.yiiframework.com/doc/guide/2.0/en/runtime-responses#sending-files )
return Yii::$app->response->sendContentAsFile(
$pdf->render(),
"download.pdf",
['inline' => true,
'mimeType' => 'application/pdf']
I'm trying to use twig template that contains my native language characters and receive this error
object(Twig_Error_Syntax)[173]
private 'lineno' (Twig_Error) => int 1
private 'name' (Twig_Error) => string 'sms' (length=3)
private 'sourceCode' (Twig_Error) => string 'Привет, {{name}}' (length=22)
protected 'message' => string 'Unexpected token "end of template" of value "" in "sms" at line 1.' (length=66)
private 'string' (Exception) => string '' (length=0)
protected 'code' => int 0
protected 'file' => string '/var/www/bitrix/vendor/twig/twig/lib/Twig/ExpressionParser.php' (length=62)
protected 'line' => int 201
However the same code works fine with Latin characters.
The code I use follows:
$loader = new Twig_Loader_Array(['sms' => $arTemplate['PROPERTY_SMS_VALUE']['TEXT'], 'mail' => $arTemplate['PROPERTY_EMAIL_VALUE']['TEXT']]);
$twig = new Twig_Environment($loader);
try {
$sms_text = $twig->render('sms', ['name' => $_REQUEST[FIELD_NAME]]);
It basically takes twig from database and applies simple render. I use twig without Symfony (only basic requirements installed)
It would be better to use tools.twig for bitrix CMS.
composer req maximaster/tools.twig
Then you should delete template.php and create template.twig file in component folder.
In the docs you can find bitrix variables available in the twig template and rule for template inheritance.
$output = $modx->runSnippet('getImageList',array(
'tvname' => 'workOrders',
'where' => $_GET['search'] ,
'tpl' => 'workOrdersList',
'docid' => 3
));
One of the fields is a string with parameters. How can I check whether my search string is a part of that field? I have looked up how to use "where" parameter to accomplish this task but I am still stuck.
If you just use this extra (getUrlParam), you can call this instead of refering to the GET directly:
So your call could look like this:
$output = $modx->runSnippet('getImageList', array(
'tvname' => 'workOrders',
'where' => $modx->runSnippet('getUrlParam', array('name' => 'search`)),
'tpl' => 'workOrdersList',
'docid' => 3
));
This also takes care of malicious url parameters.
Where needs to be formatted as a JSON value, so you would need to define which field you are querying against and format as JSON. E.g. 'where' => $modx->toJSON(array('pagetitle'=>$_GET['search']))
I am using swftools to display filefields (Drupal 6).
Now I would like to use swftools to display the custom-url emfields (embedded media fields). For example, if my emaudio field contains the url http://example.com/myaudio.mp3. I would like to use the swftools audio player to play this mp3 file.
I know how to invoke the swftools player, if I am displaying a node that contains an emfield. I use hook_preprocess_content_field() to replace $items[0]['view'] with swf($items[0]['value']):
function mytheme_preprocess_content_field(&$vars) {
foreach ($vars['items']as $index=>$arr){
// Note:
// Emfield's custom_url video provider is called "zzz_custom_url".
// Emfield's custom_url audio provider is called "custom_url"
if ($arr['provider']=='zzz_custom_url' || $arr['provider']=='custom_url'){
$vars['items'][$index]['view'] = swf($arr['value']);
}
}
}
But I do not know how to invoke the swf player if I am displaying a view that contains an emfield. That is, I have not been able to figure out how to pull off a similar trick when I am displaying a view rather than a node. Any suggestions?
I have a solution.
I created a views template to theme my emaudio field,
views-view-field--field-my-emaudio-embed.tpl.php
Here is the content of the template:
$data = $row->{$field->field_alias};
print swf($data);
Another solution:
Create the custom formatter "SWF Emaudio", as described below. Then select this formatter to display your custom-url emaudio field, from within the cck or views UI.
function mymodule_field_formatter_info() {
$formatters = array();
$formatters['swf_emaudio'] = array(
'label' => t('SWF Emaudio'),
'field types' => array('emaudio'),
);
}
function mymodule_theme() {
$themes['mymodule_formatter_swf_emaudio'] = array(
'arguments' => array('element' => NULL, 'options' => array()),
'function' => 'theme_o4_mediatools_formatter_swf_emaudio',
);
}
return ($themes);
}
function theme_mymodule_formatter_swf_emaudio($element, $options = array()) {
$embed_value = $element['#item']['value'] ;
$output = swf($embed_value, $options);
return ($output);
}
I am handling file upload field in a form using Drupal 6 form APIs. The file field is marked as required.
I am doing all the right steps in order to save and rename files in proper locations.
upload form
$form = array();
....
$form['image'] = array(
'#type' => 'file',
'#title' => t('Upload photo'),
'#size' => 30,
'#required' => TRUE,
);
$form['#attributes'] = array('enctype' => "multipart/form-data");
...
form validate handler
$image_field = 'image';
if (isset($_FILES['files']) && is_uploaded_file($_FILES['files']['tmp_name'][$image_field])) {
$file = file_save_upload($image_field);
if (!$file) {
form_set_error($image_field, t('Error uploading file'));
return;
}
$files_dir = file_directory_path();
$contest_dir = 'contest';
if(!file_exists($files_dir . '/' . $contest_dir) || !is_dir($files_dir . '/' . $contest_dir))
mkdir($files_dir . '/' . $contest_dir);
//HOW TO PASS $file OBJECT TO SUBMIT HANDLER
$form_state['values'][$image_field] = $file;
file_move($form_state['values'][$image_field], $files_dir."/" . $contest_dir . "/contest-". $values['email']. "-" . $file->filename);
}
else {
form_set_error($image_field, 'Error uploading file.');
return;
}
On submiting form
Form always reports an error Upload photo field is required. although files are getting uploaded. How to deal with this issue?
How to pass file information to submit handler?
your handler is wrong. You never should touch $_FILES or $_POST variables in drupal, instead you should only use the drupal tools. Said that, the implementation you should is like that:
function my_form_handler(&$form,&$form_state){/** VALIDATION FILE * */
$extensions = 'jpeg jpg gif tiff';
$size_limit = file_upload_max_size();
$validators = array(
'my_file_validate_extensions' => array($extensions),
'my_file_validate_size' => array($size_limit),
);
$dest = file_directory_path();
if ($file = file_save_upload('image', $validators, $dest)) {
//at this point your file is uploaded, moved in the files folder and saved in the DB table files
}
}
I think you'll want to use the filefield module and append it to a form, as described in:
Drupal Imagfield/Filefield in custom form
The question has a link to the solution:
http://sysadminsjourney.com/content/2010/01/26/display-cck-filefield-or-imagefield-upload-widget-your-own-custom-form
From the Drupal 6 Form API docs:
"Note: the #required property is not supported (setting it to true will always cause a validation error). Instead, you may want to use your own validation function to do checks on the $_FILES array with #required set to false. You will also have to add your own required asterisk if you would like one."
Old post, but I'm looking for something similar and figured I add that.