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 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>";  
    • By sirhc
      Hi all,
      I got a Internal Server Error when accessing de /setup/fields in my back end...
      Also when i try to reach it through the page tree, i can't edit the 'fields' page, same error.
      I do modify a image field through the api, i don't know if that has something to do with it, below the code i use to upload an image through the api, I deleted the parts that are not important. If anyone can help me that would be great.
      To clarify; the code below works fine, my image and page is created as expected, the only thing is that I can't access my /setup/fields/ page in my CMS anymore due to the Internal Server Error.
       
       <?php              if($input->post->submit) {             $photoPage = new Page(); // create new page object             $photoPage->template = 'photo'; // set template             $photoPage->parent     = wire('pages')->get('/foto/'); // set the parent                          $photoPage->name     = urlBuilder($input->title_photo); // give it a name used in the url for the page             $photoPage->title     = $input->title_photo; // set page title (not neccessary but recommended)                          $photoPage->save();                              // next: process photo upload                          // Set a temporary upload location where the submitted files are stored during form processing             $upload_path = $config->paths->assets . "files/photos/";                          // New wire upload             $file_upload = new WireUpload('file_upload'); // References the name of the field in the HTML form that uploads the photo             $file_upload->setMaxFiles(1);             $file_upload->setOverwrite(true);             $file_upload->setDestinationPath($upload_path);             $file_upload->setValidExtensions(array('jpg', 'jpeg', 'png', 'gif'));                          // execute upload and check for errors             $files = $file_upload->execute();                          // Run a count($files) test to make sure there are actually files; if so, proceed; if not, generate getErrors()             if(!count($files)) {                 $file_upload->error("Sorry, but you need to add a photo!");                 return false;             }             //success                          //echo 'photos stored';exit;                                  $photoPage->save();             //$session->redirect($locationPage->url);         } else {      ?> <section class="add-location">     <div class="container">         <div class="row">             <div class="col-xs-12">                      <h2>Spot toevoegen</h2>                 <form id="add-form" action="" enctype="multipart/form-data" method="post">                     <input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $max_file_size; ?>" />                                          <div class="col-xs-12 col-md-6">                             <label for="fileselect">Upload jouw foto:</label>                         <p>                                                          <input type="file" name="file_upload" id="file_upload" accept="image/*"  class="inputfile" />                             <label for="file_upload">                                 <span></span> <strong><i class="fa fa-upload" aria-hidden="true"></i>                                 Kies je foto…</strong>                             </label>                                                      </p>                         </div>                                                  <input type="submit" name="submit" value="Upload" />                     </div>                 </form>                  </div>         </div>     </div> </div> <?php } } include('./includes/foot.php');?>  
    • By hellomoto
      I have a manufacturers page select field and a dependent models one with pages of template `model` which are allowed children of pages with template `manufacturer`. So I have this as the findPagesCode for the models page options field:
      return $page->manufacturer->children(); However this requires the page be saved in order to display options. This is not ideal. 
      I have a singular autoload module with the following:
      public function init() { $this->pages->addHookAfter('render', $this, 'filterModels'); } public function filterModels($event) { $page = $event->arguments('page'); if($page->template != 'boat_vessel') return; $this->message("models filter"); } It's doing nothing. 
      I was thinking I could work out something with this example but I would need the above test to be working first anyway...
      $this->pages->addHookAfter('changed', function(HookEvent $event) { $page = $event->object; $change = $event->arguments(0); if($page->template == 'boat_vessel' && $change == 'manufacturer') { // execute some code } }); But what? How do I refresh the models field?
      Is there a way to do this in the field settings? I would think in the custom PHP textarea that `return $page->manufacturer->children();` would work but it doesn't. 
    • By hellomoto
      mamo_length_ft is not a field. I don't see it anywhere in the database neither. Where could this possibly be stemming from?