Problems with twig in cyrillic - twig

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.

Related

How render string <drupal-media> in Twig template?

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.

How to add common model to Twig and Slim4

I'm using Twig and Slim4 with DI container (the same as this tutorial: https://odan.github.io/2019/11/05/slim4-tutorial.html).
I would like to know how can I add a common model to all my twig views, for example user object, general options and something like this.
This is the container Twig initialization:
TwigMiddleware::class => function (ContainerInterface $container) {
return TwigMiddleware::createFromContainer($container->get(App::class), Twig::class);
},
// Twig templates
Twig::class => function (ContainerInterface $container) {
$config = $container->get(Configuration::class);
$twigSettings = $config->getArray('twig');
$twig = Twig::create($twigSettings['path'], $twigSettings['settings']);
return $twig;
},
The twig middleware is the Slim standard one: Slim\Views\TwigMiddleware
You can add global variables to Twig environment, so they are accessible in all template files:
(To be able to provide a sample code, I assumed you have defined a service like user-authentication-service which is capable of resolving current user)
// Twig templates
Twig::class => function (ContainerInterface $container) {
//...
$twig = Twig::create($twigSettings['path'], $twigSettings['settings']);
$twig->getEnvironment()->addGlobal(
'general_settings',
[
'site_name' => 'my personal website',
'contact_info' => 'me#example.com'
]);
$twig->getEnvironment()->addGlobal(
'current_user',
// assuming this returns current user
$container->get('user-authentication-service')->getCurrentUser()
);
return $twig;
},
Now you have access to general_settings and current_user in all of your template files.

serenity-js / cucumber / chai Promise AssertionError need some assistance

Im losing it, I feel like im doing this right BUT cant figure out why this simple test fails
I got a feature file like so
Scenario: List all accounts in the tenant
Given that Keith has navigated to the tenant account list
When he views the accounts in the table that include name, name, name
Then he should also see 1,2,3 in the list
I got a definition file like so
this.When(/^(.*?) views the accounts in the table that include (.*)$/, (name: string, accountInformation: string) => {
return stage.theActorCalled(name).attemptsTo(
ViewAllAccountNames.inTheTableOf(listOf(accountInformation)),
);
});
I got a pageObject file like so
export class AccountTable {
// static displayingAll = Text.ofAll(AccountListUI.accountListView);
// static isDisplayingAllNames = Text.ofAll(Target.the('account names in the table').located(by.css('table tbody tr td:nth-child(2)')));
static AccountNames = Target.the('account names in the table').located(by.css('table tbody tr td:nth-child(2)'));
static AccountNumbers = Target.the('account numbers in the table').located(by.css('table tbody tr td:nth-child(1)'));
static isDisplayingAllNames = Text.ofAll(AccountTable.AccountNames);
static isDisplayingAllNumbers = Text.ofAll(AccountTable.AccountNumbers);
}
here is my class that does the work
constructor(private accName: string[]) {
}
static inTheTableOf(accName: string) {
return new ViewAllAccountNames(accName);
}
performAs(actor: PerformsTasks): PromiseLike<void> {
return actor.attemptsTo(
See.if(AccountTable.isDisplayingAllNames, items => expect(items).to.eventually.contain(this.accName))
);
}
}
when i debug through webstorm inside class ViewAllAccountNames i get
static inTheTableOf(accName: string) { accName: Array(3)
return new ViewAllAccountNames(accName); accName: Array(3)
}
then when i get to my See.if function I get
performAs(actor: PerformsTasks): PromiseLike<void> {
return actor.attemptsTo(
See.if(AccountTable.isDisplayingAllNames, items => expect(items).to.eventually.contain(this.accName)) AccountTable.isDisplayingAllNames: undefined
);
}
so my dilemma is this:
and I think it stems to my See.if function isnt setup in the correct way?
See.if(AccountTable.isDisplayingAllNames, items => expect(items).to.eventually.contain(this.accName))
Cucumber test run has failed.
1) Scenario: List all accounts in the tenant - e2e\features\get_account_list\get_all_accounts.feature:10
Step: When he views the accounts in the table that include name, name, name - e2e\features\get_account_list\get_all_accounts.feature:12
Step Definition: node_modules\serenity-js\src\serenity-cucumber\webdriver_synchroniser.ts:47
Message:
AssertionError: expected [ Array(5) ] to include [ 'name', 'name', 'name' ]
From: Task: <anonymous>
i am an idiot, I was using the wrong function, had to use the function
.to.eventually.contain.members
similar answer and description found here:
Answer

How to stop Silverstripe SearchContext from throwing Version Table_Live error

Consider the following Silverstripe page class
<?php
class Page extends SiteTree{
static $has_many = array('OtherDataObjects' => 'DataObjectClass');
public function getSearchContext() {
$fields = new FieldSet(
new TextField('Title', 'Tour'),
new DropdownField('OtherDataObjects', 'Other Data Object', array('data', 'value')
);
$filters = array(
'Title' => new PartialMatchFilter('Title'),
'OtherDataObjects' => new PartialMatchFilter('OtherDataObjects.Title')
);
return new SearchContext(
'Page',
$fields,
$filters
);
}
}
Adding this search form to a front-end form and posting a search form always results in a [User Error] with a SQL error containing something like this at the end.
AND ("DataObjectClass_Live"."DataObjectClass_Live" LIKE 'title') ORDER BY "Sort" LIMIT 25 OFFSET 0 Table 'database DataObjectClass_Live' doesn't exist
My searchcontext search throws up an error each time I try to run a search on a has_many relationship. The versioned extension seems to be the culprit because it adds _live to all tables regardless whether the baseclass has the versioned extension or not I get the same error in SilverStripe versions 2.4.x and the latest 3.0.x versions.
Any help or pointers will be appreciated.
maybe try using an sqlQuery. something like
function SearchResults() {
$select = array('*');
$from = array('OtherDataObjects');
$where = array('OtherDataObjects:PartialMatch' => '%' . $data['Title'] . '%');
$sqlQuery = new SQLQuery($select, $from, $where);
$results = $sqlQuery->execute();
return $results;
}
$data['Title'] is to be the value from the search textbox
partial match reference: http://doc.silverstripe.org/framework/en/topics/datamodel
sql query reference: http://doc.silverstripe.org/framework/en/reference/sqlquery

drupal views: how to invoke swftools media player for emfields?

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);
}

Resources