TYPO3: HMENU not working in foreign language - localization

The footer menu is defined as follows:
temp.footerNav = HMENU
temp.footerNav {
special = userfunction
special.userFunc = \MyNamespace\Helper->footerNavArray
wrap = <ul>|</ul>
1 = TMENU
1 {
wrap = |
expAll = 0
NO = 1
NO.ATagParams = class="footer-navigation-link"
NO.stdWrap.htmlSpecialChars = 1
NO.wrapItemAndSub = <li class="footer-navigation__item">|</li>
NO.stdWrap.field = title // nav_title
}
}
The array returned by the function footerNavArray looks good in both German
array (size=7)
0 =>
array (size=2)
'title' => string 'Unternehmen' (length=11)
'_OVERRIDE_HREF' => string 'de/unternehmen.html' (length=19)
...
...
and English:
array (size=7)
0 =>
array (size=2)
'title' => string 'Company' (length=7)
'_OVERRIDE_HREF' => string 'en/company.html' (length=15)
...
...
The footer menu works perfectly in English (default language), however, the only output I can see on the German page (L=1) is <ul></ul>.
Any ideas?

First of all, the above behaviour was caused by [FE][hidePagesIfNotTranslatedByDefault] = 1, which is required by Solr for TYPO3 to work correctly in a multi-language site. By adding '_SAFE' = true to the menu array, I was finally able to solve the problem:
array (size=7)
0 =>
array (size=3)
'title' => string 'Company' (length=7)
'_OVERRIDE_HREF' => string 'en/company.html' (length=15)
'_SAFE' => boolean true
...
...
I've found the solution in line 1213 (core version 7.6.2) of TYPO3\CMS\Frontend\ContentObject\Menu\AbstractMenuContentObject->filterMenuPages():

you can also use global env condition for different language.
#setup the default language in case of bad L variable
config.sys_language_mode = content_fallback
config.uniqueLinkVars=1
config.sys_language_overlay = 1
config.sys_language_uid = 0
config.language = en
config.linkVars = L
# Spanish language, sys_language.uid = 1
[globalVar = GP:L = 1]
config.sys_language_uid = 1
config.language = es
config.locale_all = spanish
[global]
# English language, sys_language.uid = 0
[globalVar = GP:L = 0]
config.sys_language_uid = 0
config.language = en
[global]

Related

How can I shift all of the tables down after removing a table?

In this code:
t = {
num = '',
}
t[0].num = '0'
t[1].num = '1'
t[2].num = '2'
Is there a way for me to delete t[0], then shift all of the table's values down, so that afterword it looks like this:
t[0].num = '1'
t[1].num = '2'
Example with imaginary functions:
t = {
num = '',
}
t[0].num = '0'
t[1].num = '1'
t[2].num = '2'
for i=0,tableLength(t) do
print(t[i])
end
--Output: 012
remove(t[0])
for i=0,tableLength(t) do
print(t[i])
end
--Output: 12
t = {
num = '',
}
t[0].num = '0'
t[1].num = '1'
t[2].num = '2'
This code will cause errors for indexing t[0], a nil value.
t only has one field and that is t.num
You need to do something like this:
t = {}
for i = 0, 2 do
t[i] = {num = tostring(i)}
end
if you want to create the desired demo table.
As there are many useful functions in Lua that assume 1-based indexing you I'd recommend starting at index 1.
local t = {1,2,3,4,5}
Option 1:
table.remove(t, 1)
Option 2:
t = {table.unpack(t, 2, #t)}
Option 3:
t = table.move(t, 2, #t, 1, t)
t[#t] = nil
Option 4:
for i = 1, #t-1 do
t[i] = t[i+1]
end
t[#t] = nil
There are more options. I won't list them all. Some do it in place, some result in new table objects.
As stated in this answer, by creating a new table using the result of table.unpack:
t = {table.unpack(t, 1, #t)}

Pulling XML tags with Lua

I'm currently working on an XML parser and I'm trying to use Lua's pattern matching tools but I'm not getting the desired result. Let's say I have this XML snippet:
<Parent>
<Child>
<Details>Text in Parent tag and Details child tag</Details>
<Division>Text in Parent tag and Division child tag</Division>
</Child>
</Parent>
I need to pull the Parent tag out into a table, followed by any child tags, and their corresponding text data. I already have the pattern for pulling the data figured out:
DATA = "<.->(.-)<"
Likewise for pulling tags individually:
TAGS ="<(%w+)>"
However like I mentioned, I need to differentiate between tags that are nested and tags that aren't. Currently the pattern that's getting the closest result I need is:
CHILDTAG= "<%w->.-<(%w-)>"
Which should print only "Child" but it prints "Division" as well for a reason I can't comprehend. The idea behind the CHILDTAG pattern is it captures a tag IFF it had an enclosing tag, i.e , the ".-" is there to signify that it may/may not have a new line between it, however I think that's completely wrong because \n- doesn't work and that signifies a new line. I referred to the documentation and to:
https://www.fhug.org.uk/wiki/wiki/doku.php?id=plugins:understanding_lua_patterns
I use Lua 5.1. I want to parse an XML file of the following pattern. How should I go about it?
Lua XML extract from pattern
Simple XML parser (Named entities in XML are not supported)
local symbols = {lt = '<', gt = '>', amp = '&', quot = '"', apos = "'", nbsp = ' ', euro = '€', copy = '©', reg = '®'}
local function unicode_to_utf8(codepoint)
-- converts numeric unicode to string containing single UTF-8 character
local t, h = {}, 127
while codepoint > h do
local low6 = codepoint % 64
codepoint = (codepoint - low6) / 64
t[#t+1] = 128 + low6
h = 288067 % h
end
t[#t+1] = 254 - 2*h + codepoint
return string.char((table.unpack or unpack)(t)):reverse()
end
local function unescape(text)
return (
(text..'<![CDATA[]]>'):gsub('(.-)<!%[CDATA%[(.-)]]>',
function(not_cdata, cdata)
return
not_cdata
:gsub('%s', ' ')
--:gsub(' +', ' ') -- only for html
:gsub('^ +', '')
:gsub(' +$', '')
:gsub('&(%w+);', symbols)
:gsub('&#(%d+);', function(u) return unicode_to_utf8(to_number(u)) end)
:gsub('&#[xX](%x+);', function(u) return unicode_to_utf8(to_number(u, 16)) end)
..cdata
end
)
)
end
function parse_xml(xml)
local tag_stack = {}
local result = {find_child_by_tag = {}}
for text_before_tag, closer, tag, attrs, self_closer in xml
:gsub('^%s*<?xml.-?>', '') -- remove prolog
:gsub('^%s*<!DOCTYPE[^[>]+%[.-]>', '')
:gsub('^%s*<!DOCTYPE.->', '')
:gsub('<!%-%-.-%-%->', '') -- remove comments
:gmatch'([^<]*)<(/?)([%w_]+)(.-)(/?)>'
do
table.insert(result, unescape(text_before_tag))
if result[#result] == '' then
result[#result] = nil
end
if closer ~= '' then
local parent_pos, parent
repeat
parent_pos = table.remove(tag_stack)
if not parent_pos then
error("Closing unopened tag: "..tag)
end
parent = result[parent_pos]
until parent.tag == tag
local elems = parent.elems
for pos = parent_pos + 1, #result do
local child = result[pos]
table.insert(elems, child)
if type(child) == 'table' then
--child.find_parent = parent
parent.find_child_by_tag[child.tag] = child
end
result[pos] = nil
end
else
local attrs_dict = {}
for names, value in ('\0'..attrs:gsub('%s*=%s*([\'"])(.-)%1', '\0%2\0')..'\0')
:gsub('%z%Z*%z', function(unquoted) return unquoted:gsub('%s*=%s*([%w_]+)', '\0%1\0') end)
:gmatch'%z(%Z*)%z(%Z*)'
do
local last_attr_name
for name in names:gmatch'[%w_]+' do
name = unescape(name)
if last_attr_name then
attrs_dict[last_attr_name] = '' -- boolean attributes (such as "disabled" in html) are converted to empty strings
end
last_attr_name = name
end
if last_attr_name then
attrs_dict[last_attr_name] = unescape(value)
end
end
table.insert(result, {tag = tag, attrs = attrs_dict, elems = {}, find_child_by_tag = {}})
if self_closer == '' then
table.insert(tag_stack, #result)
end
end
end
for _, child in ipairs(result) do
if type(child) == 'table' then
result.find_child_by_tag[child.tag] = child
end
end
-- Now result is a sequence of upper-level tags
-- each tag is a table containing fields: tag (string), attrs (dictionary, may be empty), elems (array, may be empty) and find_child_by_tag (dictionary, may be empty)
-- attrs is a dictionary of attributes
-- elems is a sequence of elements (with preserving their order): tables (nested tags) or strings (text between <tag> and </tag>)
return result
end
Usage example:
local xml= [[
<Parent>
<Child>
<Details>Text in Parent tag and Details child tag</Details>
<Division>Text in Parent tag and Division child tag</Division>
</Child>
</Parent>
]]
xml = parse_xml(xml)
--> both these lines print "Text in Parent tag and Division child tag"
print(xml[1].elems[1].elems[2].elems[1])
print(xml.find_child_by_tag.Parent.find_child_by_tag.Child.find_child_by_tag.Division.elems[1])
What parsed xml looks like:
xml = {
find_child_by_tag = {Parent = ...},
[1] = {
tag = "Parent",
attrs = {},
find_child_by_tag = {Child = ...},
elems = {
[1] = {
tag = "Child",
attrs = {},
find_child_by_tag = {Details = ..., Division = ...},
elems = {
[1] = {
tag = "Details",
attrs = {},
find_child_by_tag = {},
elems = {[1] = "Text in Parent tag and Details child tag"}
},
[2] = {
tag = "Division",
attrs = {},
find_child_by_tag = {},
elems = {[1] = "Text in Parent tag and Division child tag"}
}
}
}
}
}
}

Ruby - getting value in logstash

i have a field and value output looks like
A::field_1_2_3_4_22_5_6_7_8_365 => 6
because the field name is "dynamical" because of contain ip and port.
how to using ruby to get value from field name and add field for it ? it's will be looks like
A::field => 6
bIP => 1.2.3.4
bport => 22
cIP => 5.6.7.8
cport => 365
any help will be appreciated!!thanks
Here is an answer for a very similar question: logstash name fields dynamically
For this precise case, the ruby filter needs to be a bit more involved in order to capture different things
filter {
ruby {
code => "
newhash = {}
event.to_hash.each {|key, value|
re = /(\w::[a-z]+)_(\d+_\d+_\d+_\d+)_(\d+)_(\d+_\d+_\d+_\d+)_(\d+)/
if key =~ re then
field, bIP, bport, cIP, cport = key.match(re).captures
newhash[field] = event[key]
newhash['bIP'] = bIP.gsub('_', '.')
newhash['bport'] = bport
newhash['cIP'] = cIP.gsub('_', '.')
newhash['cport'] = cport
event.remove(key)
end
}
newhash.each {|key,value|
event[key] = value
}
"
}
}
So if you have a field like "A::field_1_2_3_4_22_5_6_7_8_365" => "6" in your event, your event will then contain the following fields:
{
"A::field" => "6",
"bIP" => "1.2.3.4",
"bport" => "22",
"cIP" => "5.6.7.8",
"cport" => "365"
}
You can use the the following code:
field_name = 'A::field_1_2_3_4_22_5_6_7_8_365'
fields = field_name.split("_")
bID = "#{fields[1]}.#{fields[2]}.#{fields[3]}.#{fields[4]}"
bport = "#{fields[5]}"
bID = "#{fields[6]}.#{fields[7]}.#{fields[8]}.#{fields[9]}"
bport = "#{fields[10]}"
Maybe i've not understooden this quite correctly but you can use arrays and method .push for adding:
Array=[1, 2, 5, 7]
#n = [3, 4, 6]
Array.push(#n) => Array=[1, 2, 3, 4, 5, 6, 7]

Wordpress if custom field value is null previous posts' value is displayed

I have this code with a loop that gives me some trouble and strange results.
This particular post type has a few custom fields which I put in an array. Everything works as expected, but if a field has no value it will take the value from the previous post within the loop.
Let's say I have these posts in Wordpress:
Post ID 10
custom_1 = 10
custom_2 = 20
custom_3 = 30
Post ID 20
custom_1 = 40
custom_2 = null
custom_3 = null
If I run my loop I get these results
Post ID 10
custom_1 = 10
custom_2 = 20
custom_3 = 30
Post ID 20
custom_1 = 40
custom_2 = 20 (instead of null)
custom_3 = 30 (instead of null)
Here's the short version of that loop:
$query = new WP_Query($query_arg);
if ($query->have_posts()) {
while ($query->have_posts()) {
$query->the_post();
$result[] = array(
"custom_1" => get_post_meta($post->ID, 'custom_1', true),
"custom_2" => get_post_meta($post->ID, 'custom_2', true),
"custom_3" => get_post_meta($post->ID, 'custom_3', true)
);
}
}
wp_reset_postdata();
I can't seem to wrap my head around this one.. tried pretty much everything, but nothing seams to work.
Does anyone know what's happening here?
UPDATE:
Fixed it by changing the loop to this.
$query = new WP_Query($query_arg);
if ($query->have_posts()) {
while ($query->have_posts()) {
$query->the_post();
//set vars to "" which 'resets' the value with every new post in the loop
$custom_1 = "";
$custom_2 = "";
$custom_3 = "";
//set vars to the value of the custom fields
$custom_1 => get_post_meta($post->ID, 'custom_1', true);
$custom_2 => get_post_meta($post->ID, 'custom_2', true);
$custom_3 => get_post_meta($post->ID, 'custom_3', true);
$result[] = array(
"custom_1" => $custom_1,
"custom_2" => $custom_2,
"custom_3" => $custom_3
);
}
}
wp_reset_postdata();
It might be due to how the MYSQL handles null. Null means an absence of value, so they do not get a space in the database, not even an empty one. An empty string however does show in the database.
Try the following
Post ID 10
custom_1 = 10
custom_2 = 20
custom_3 = 30
Post ID 20
custom_1 = 40
custom_2 = ""
custom_3 = ""

Lua iterator to array

In Lua parlance, is there any syntactic sugar for turning an iterator function into an array (repeated invocations with results stored in ascending indices), perhaps something in the standard library ?
I'm tokenizing a string belonging to a protocol and need to to have positional access to elements at the start of the string, and the end of the string is a variant collection.
The code (specific to my use-case) is as follows, I find it hard to believe that it isn't in the standard library :d
local array_tokenise = function (line)
local i = 1;
local array = {};
for item in string.gmatch(line,"%w+") do
array[i] = item;
i = i +1
end
return array
end
There's no standard library function for it. But really, it's pretty trivial to write:
function BuildArray(...)
local arr = {}
for v in ... do
arr[#arr + 1] = v
end
return arr
end
local myArr = BuildArray(<iterator function call>)
This will only work if your iterator function returns single elements. If it returns multiple elements, you'd have to do something different.
As Nicol Bolas said, there is no standard library function that performs the action you desire.
Here is a utility function that extends the table library:
function table.build(build_fn, iterator_fn, state, ...)
build_fn = (
build_fn
or function(arg)
return arg
end
)
local res, res_i = {}, 1
local vars = {...}
while true do
vars = {iterator_fn(state, vars[1])}
if vars[1] == nil then break end
--build_fn(unpack(vars)) -- see https://web.archive.org/web/20120708033619/http://trac.caspring.org/wiki/LuaPerformance : TEST 3
res[res_i] = build_fn(vars)
res_i = res_i+1
end
return res
end
Here is some example code demonstrating usage:
require"stringify"
local t1 = {4, 5, 6, {"crazy cake!"}}
local t2 = {a = "x", b = "y", c = "z"}
print(stringify(table.build(nil, pairs(t1))))
print(stringify(table.build(nil, pairs(t2))))
print(stringify(table.build(
function(arg) -- arg[1] = k, arg[2] = v
return tostring(arg[1]).." = "..tostring(arg[2])
end
, pairs(t1)
)))
local poetry = [[
Roses are red, violets are blue.
I like trains, and so do you!
By the way, oranges are orange.
Also! Geez, I almost forgot...
Lemons are yellow.
]]
print(stringify(table.build(
function(arg) -- arg[1] == plant, arg[2] == colour
return (
string.upper(string.sub(arg[1], 1, 1))..string.lower(string.sub(arg[1], 2))
.. " is "
.. string.upper(arg[2]).."!"
)
end
, string.gmatch(poetry, "(%a+)s are (%a+)")
)))
Output:
{
[1] = {
[1] = 1,
[2] = 4,
},
[2] = {
[1] = 2,
[2] = 5,
},
[3] = {
[1] = 3,
[2] = 6,
},
[4] = {
[1] = 4,
[2] = {
[1] = "crazy cake!",
},
},
}
{
[1] = {
[1] = "a",
[2] = "x",
},
[2] = {
[1] = "c",
[2] = "z",
},
[3] = {
[1] = "b",
[2] = "y",
},
}
{
[1] = "1 = 4",
[2] = "2 = 5",
[3] = "3 = 6",
[4] = "4 = table: 00450BE8",
}
{
[1] = "Rose is RED!",
[2] = "Violet is BLUE!",
[3] = "Orange is ORANGE!",
[4] = "Lemon is YELLOW!",
}
stringify.lua can be found here
if you are just looking to auto-increment the table key for each data element, you can use table.insert(collection, item) - this will append the item to the collection and sets the key to the collection count + 1

Resources