Skip to content
Grav 2.0 is officially stable. Read the announcement →
General

Get array elements from selectize field

Started by Pedro M 4 years ago · 4 replies · 435 views
4 years ago

Hi.

I'd like to get the values from a selectize field.
I have this in item.yaml:

YAML
header.author:
   type: selectize
   multiple: true
   size: medium
   label: Author

In blog-item.html.twig I have:

TWIG
{% if page.header.author %}
    {% set authors = page.header.author.get() %}
{% else %}
    {% set authors =  taxonomylist.get() %}
{% endif %}

{% for author,value in authors['author'] %}
   <a href="{{ blog.url|rtrim('/') }}/author{{ config.system.param_sep }}{{ author }}" itemprop="keywords">{{ tag }}<span rel="author"> {{ author }} </span></a>
{% endfor %}

I can get the values from the author taxonomy in options tab, but I don't know how I can get that values from the page.header.author (in blog item tab). This is a selectize field. The result in item.md is:

YAML
taxonomy:
  tag:
    - tag1
  author:
    - author1  
author:
    - author2

I'd also like to be able to get the values from header.author, in case the user doesn't use the author taxonomy in options tab.

Any idea for this (sorry, I'm begginer with PHP and Twig)
Thanks in advanced.

4 years ago

What happens if you change two lines

TWIG
    {% set authors =  taxonomylist.author.get() %}

and

TWIG
{% for author,value in authors %}

I'm not at my PC now, but looking at code I see you have a level of author in taxonomy, but there's no another level of author in author, which you try to loop through

4 years ago

Hi @Karmalakas

Your suggestion not work. Nevertheless, I not have problem with values from taxonomylist ( {% set authors = taxonomylist.get() %}, work fine ).
I'd like to get values form selectize field as array elements, but I don't know the way to do this.

4 years ago

You're looping through authors.author
In header taxonomy you have taxonomy.author
In header author you have just author
When you get authors from header taxonomy, you end up with authors.author
When you get authors from header author, you end up with authors
In second case you don't have authors.author to loop through.

What was your code after my suggestion?

4 years ago

Hi @Karmalakas

In the end I have chosen to use the author taxonomy, instead of using the header.author blueprint in the .yaml template. The final code is the following:

TWIG
{% if config.plugins.taxonomylist.enabled and page.taxonomy.author is not empty %}
 <span><i class="fas fa-user"></i></span>
 {% for author in page.taxonomy.author %}
     <a href="{{ blog.url|rtrim('/') }}/author{{ config.system.param_sep }}{{ author }}" rel="author" itemprop="keywords">{{ author }}</a>
     {% if not loop.last %}, {% endif %}
 {% endfor %}
{% endif %}

However, it is easy to get the values of a selectize field array with the function: "array(page.header.author)"

Suggested topics

Topic Participants Replies Views Activity
General · by Hanns Mattes, 9 hours ago
1 8 2 hours ago
General · by Andy Miller, 2 hours ago
0 4 2 hours ago
General · by Jerry Hunt, 5 days ago
2 136 1 day ago
General · by pamtbaau, 1 day ago
1 93 1 day ago
General · by Andy Miller, 2 days ago
0 77 2 days ago