How to use substr with strstr in Twig - symfony

I'm working on a Symfony site which incorporates a scheduler, and on this scheduler there are tasks which have user's names on them. My client has requested that only the surname be shown but currently the database saves the name as a complete string named contactName.
I am currently using the following code in Twig to display the name:
schedule.user.contactName
I tried using the split command, which seemed to do what I want:
schedule.user.contactName|split(" ",1)
But this only returns an array, and I do not know how to take the surname from this.
Any help with this is appreciated - maybe there is an alternative way to do this?

Maybe you could try this:
schedule.user.contactName|split(' ', 2)|first

There are two ways I can think of:
Set the output of the split function as a new variable, and since it's an array, you should be able to get name[1], or maybe name.1 from it for the 2nd part (if it exists, and your customer is not 'Cher', 'Prince', or the like).
It's probably easier to do it in two parts like this, rather than trying to combine it to a single statement.
{% set nameParts = schedule.user.contactName|split(" ",1) %}
{{ nameParts[1]|default(schedule.user.contactName) }}
You can also perform the split within the entity - getContactNameSurname() that does the split in PHP code, and returns the 2nd part.

Related

Nested Shortcodes?

For my WordPress site, I am using both PODS, and WPDataTables to manage my data. With that, I am using WPDT's "placeholder" feature to create dynamic SQL queries that will display different data depending on what user is logged in. Currently, I can ALMOST do everything I need, but not quite. This is more or less what I would like to achieve (var1 = the output of another shortcode):
[wpdatatable id=14 var1=
[pods name="User" template="Company" where="id = {#user.id}"][/pod]]
[/wpdatatable]
This shortcode is working, and will pull back a single number, based on the user that is logged in:
Company: [pods name="User" template="Company" where="id = {#user.id}"]
And this code is working if I hardcode a specific value to var1:
[wpdatatable id=14 var1=5]
Basically, I have two separate shortcodes that work individually, but will not work when put together. I was wondering if something like this would be possible? Or if I needed to maybe think of a new approach. Thanks in advance!
Side Note: I have tried using the "Outerbridge Nested Shortcodes" plug-in without any luck. After talking with the plug-in author, I discovered that this plugin only works for:
[tag-a] [tag-b] [/tag-a]
And not:
[tag-a [tag-b] ]
That's something wpdatatable needs to enable like we do it for pods but that would be around the other way see how it's done for pods: https://pods.io/docs/build/using-shortcodes-pods-templates/

multiple display of the same form_widget(form)

I would like to display a uniquely defined form_widget(form.myform) at several places in my code.
How could I do it?
When just put form_widget(form.myform) at different places in my code, only the first one is shown, all the following ones are ignored.
Thank you!
It is impossible by following reason: by default twig generates id for each form field considering following scheme - formTypeName_formTypeFieldName, and there could be only one element with such id at the same time.
EDIT:
You could try something like {{ form_widget(form.title, { 'id': 'my_custom_id' }) }} with different identeficators for same field, but those input still will have same names and to be frank I'm doesn't sure how it will be handled by symfony kernel.

Xquery optimization

I have this xquery as follows:
declare variable $i := doc()/some-element/modifier[empty(modifier-value)];
$i[1]/../..;
I need to run this query on Marklogic's Qconsole where we have 721170811 records. Since that is huge number of record, I am getting timeout error. Is there any way I can optimize this query to get the result?
P.S. I cannot request amdin to increase the timeout time.
Try creating an element range index (or a path range index if the target element is not unique) and using a cts:values() lexicon lookup.
That way, the request can read the values from the range index instead of having to read each document.
See:
http://docs.marklogic.com/guide/search-dev/lexicon
You could use xdmp:spawn, create a library when you will make the query, get the documents, iterate the result collecting 1000 documents per iteration and call another xdmp:spawn to process the information from that dataset, I would suggest summarize the result to return only the information you will need to don't crash the browser, at the end should look something like this:
xdmp:spawn("process.xqy")
into the library process.xqy
function local:start-process(){
let $docs := (....)
let $temp := for $x in $docs[$start to $end]
return local:process-dataset($temp) (: Could use spawn here too if you want :)
return xdmp:spawn("collect.xqy",$temp)
}
local:start-process()
compact-data function should create a file or a set of files with your data, this way the server will run all the process and in some minutes you will be available to see your data without problems.
You don't want to run something like doc() or xdmp:directory - just returns a result set that will kill you every time. You need to lower your result set by a lot.
A few thoughts:
You want to have as much done in MarkLogic's d-node, and the least work done in the e-node as possible. This is a way over-generalization, but for the most part I look at it like d-node stuff is data, indexes, lexicon work, etc. e-node stuff handles xQuery and such. So, in your example, you're definitely working out the e-node more than you need to.
You're going to want to use cts:search, as it uses indexes, not xPath to resolve your query. So, something like this:
declare variable $i := cts:search(fn:collection(),
cts:element-query(xs:QName("some-element"),
cts:element-value-query(xs:QName("modifier"), "", "exact")
)
)[1];
This will return document-node's, which it looks like what you were wanting with the $i[1]/../... This searches the xPath some-element for a modifier that is empty.
Please create element range index and attribute range index and use cts:search if you are familiar with marklogic it will be easy for you to write the query.

TAPi18n: how to construct the key to be translated dynamically?

I'm trying to add a prefix to the keys to be translated. This is one of my attempts:
<label for="{{n}}">{{_ 'input_label_{{n}}' }}</label>
<input name="{{n}}" placeholder="{{_ 'input_placeholder_{{n}}' }}">
Obviously it does not work because you can't nest {{ ... }}'s.
I've also tried creating a helper:
{{tr 'input_label' n}}
tr: function(prefix, fieldName) {
return TAPi18next.t(prefix + '_' + fieldName);
}
But it comes back untranslated. I assume because I'm calling TAPi18n as a static and not an instance of it, but I don't know how else to do it.
These are just two of many attempts.
I will have hundreds of inputs, and I want to avoid sending all the translation keys to the input template, since it's redundant information. The key variations can easily be made by adding prefixes.
Can you think of any way to generate the key values dynamically?
Use the second approach, but with TAPi18n.__ rather than TAPi18next.t.
Unlike TAPi18next.t, TAPi18n.__ reactively updates when the user's language choice is changed and the language has finished downloading. I'm guessing the problem is that the language choice hasn't quite come through when the template is first rendered, so TAP18next.t returns the strings in the default language (English), and doesn't update when the language changes. Using TAPi18n.__ instead fixes this.

Using XQuery with multiple collection functions

I am using the following XQuery to query a collection of files:
for $files in collection("/data?select=*data*.xml")
Each file in the directory has a specific name, which enables me to recognize it. I use this as the identifier, which I retrieve as follows:
let $file-id := tokenize(base-uri($files), "/")[last()]
The $file-id variable follows a certain pattern: abc-1234. The first eight characters are relevant, so I fetch them using the variable below:
let $file-link-id := substring($file-id, 1, 8)
Now, I have another collection of files, which I want to query. These files follow the same pattern in the name, because they contain connected information.
How can I use the $file-link-id to select the correct file in the second collection?
I assume I would have to include it in the second collection clause, something along the lines of ?select=$file-link-id.xml, but I am unsure of how to do this.
Maybe you could clearify your problem statement if I assume wrong here, because your problem seems to be very easy solveable (so maybe I misunderstood your problem).
So you have your correct $file-link-id and want to use it as string. If your xquery processor supports XQuery 3.0 you can use two pipes, i.e.
for $files in collection("/data?select=" || $file-link-id || ".xml")
If not, use string-join():
for $files in collection(string-join(("/data?select=", $file-link-id, ".xml"), ''))

Resources