gebeer

Find all fields that are multi language

3 posts in this topic

Hello,

I would like to find all fields that are multi-language.

My code

$langFields = new FieldsArray;
foreach ($fields as $f) {
	if($f->type instanceof FieldtypePageTitleLanguage || $f->type instanceof FieldtypeTextareaLanguage || $f->type instanceof FieldtypeTextLanguage) {
		$langFields->add($f);
	}
}

Is there a more generic way of how I can determine whether a field is multi language, other than checking  "$f->type instanceof" for all three fieldtypes?

Share this post


Link to post
Share on other sites

Untested, but every multi language field class should implement FieldtypeLanguageInterface, which you should be able to check for using instanceof.

if($f->type instanceof FieldtypeLanguageInterface) $langFields->add($f);
2 people like this

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By Brian Scramlin
      Hey!
      Quick question, 
      Should I limit how many find() methods I use on a single page? Is it fairly resource-intensive?
      I am getting consistent 
      2006 MySQL server has gone away errors and have followed all the suggestions I can find, but wonder if I am just "overloading" my server resources with database requests or something like that?
      Thank you!
    • By nbcommunication
      Hi,
      I'm sure this is maybe in the works already, given that findMany() is a recent addition to the API, but having this (and the other new find options) available to $users would be a great addition.
      Cheers,
      Chris
      NB Communication
       
    • By neonwired
      I have a category template and each page can be associated with multiple categories using a page field type.
      i have this in php : - 
      $categories = $pages->find("template=category, limit=6"); I want to sort the results by the amount of pages each category has, is there a way to do this in a selector or is there a better way of doing this?
       
    • By Marcel Stäheli
      Hello
      if have a few article pages that have a multi-page reference field with tags about the content of the article. All tags of a given page are displayed and link to a tag-overview page.
      Every link has a query string added with the tag name like &tag=cars so that I know which articles have to be displayed on the tag-overview page,
      The tag name is created using $tag->name. Since the website is multi language using German (default) and English, I use $page->localName($language) to get the localized page name . The code looks like this:
      foreach($tags as $tag) { $tagsMarkup .= "<a href='" . wire('pages')->get('/tags-overview/')->url . "?tag=" . $tag->localName(wire('user')->language) . "'>" . $tag->title . "</a>, "; }
      The code works mostly fine, but when a page has the same name in both languages, the query string will have no value just ?tag=. Why?
       
      A second problem that might be tied to the first:
      Once on that tags-overview page, I check if the tag actually exists using:
       
      $allTags = $pages->get("/tags/")->children); if($allTags->has("name=$tag") {...} But as I read on the forum (https://processwire.com/talk/topic/2979-multi-language-page-names-urls/?page=6) that only checks for name in the default language. It was suggested to use $page->parent->path . "pageNameEnglish" but I'm not sure where and how to use it in my case.
      Any help?
       
    • By xxxlogiatxxx
      Hey guys. Need some little help for a newbie. How can i foreach all possible values from existing field of a child template into select form? Trying like this but it showing me results from created pages and their fields.
      $fields = $page->find("template=item, sort=item_type"); echo "<select>"; foreach($fields as $item) { echo " <option>{$item->item_type->title}</option> "; } echo "</select>";