Jump to content

API and Cheatsheet


bwakad
 Share

Recommended Posts

It might be me - and for people who know - this may sound stupid, but I would like to have a little more information about the API/Cheatsheet and how to use this.

As an example: $fields->get("name");

I think I have to use it as this:

echo $fields->get("select_province"); // returns me the name of the field as in the back-end configured

echo $fields->get(select_province); // returns me the name of the field as in the back-end configured

So basically the same ???

But looking further...

$fields->get("selector"); // no idea what to do here

(string) $field; // no idea what to do here

$field->get($key); // no idea what to do here

$field->set($key, $value); // no idea what to do here

Inside "selector" - do I need to make something as : $pages->find ....?

inside (string) - do I need to place a text or something?

what does $key and $value represents, or do I need to replace that with something?

Is $fields, $pages, $page - predifined? and is for example "selector" and $key not predifined?

And this goes on and on, since I can not really find an explenation of what you have to fill in, and in which part, or how to use those.

While I am trying to understand and learn, a redirection to this cheatsheet is simply not much help.

Basically I am saying to also consider people who have no clue and try to learn.

Link to comment
Share on other sites

$fields->get("name") returns a Field object with the name "name".

The name set to the field in the backed creates a table in the database with field_ prepended to it. Fields->get($name) is the lowest level of getting the field. (Read, not in template context.)

$page->template->fields->name, here you get the field in context of the page, and thus in context of the template, easier is it to get the field from the Page object directly: $page->get("field") or $page->name-of-the-field. Every field can has his own label, description size settings etc etc. depending on  the context where it is used. So this is why the title field could have a label Title, and in an other template the label Product Name.

Label, description and other stuff is inherited from the field without the context, you could override it in the template context. 

$fields->get("selector"); // get a field object, rare to need this on template level and easy websites.

(string) $field; // it's a variable with the type of string, type casted to be sure it is a string.

$field->get($key); // get a object property methode etc. of the field rare to need this on template level and easy websites.

$field->set($key, $value); Setting values to the field object, these must be of the same type of the object you're setting the values. Rare you need this one to.

  • Like 2
Link to comment
Share on other sites

You can use selectors as you would use to retrieve pages. Only in this case you need to use the parameters that the current field contains.

You can always do a var_dump on a certain object to see what parameters and variables it holds.

echo "<pre>";
$key = 1;
echo "1) ". $fields->get($key)->name ."\n"; // key = 1
echo "2) ". $fields->get("id=1")->name ."\n"; // selector: id=1
echo "3) ". $fields->get("name=title")->label ."\n"; // selector: name=title
echo "4) ". $fields->get("name=title")->type ."\n";// selector: name=title

$field = $fields->get(1);
$field->set("label","Enter a title"); // key: label, value: Enter a title

echo "5) ". $fields->get("name=title")->label ."\n";// selector: name=title

var_dump($fields->get("id=1"));

1) title

2) title
3) Title
4) FieldtypePageTitle
5) Enter a title

object(Field)[159]
  protected 'settings' =>
    array (size=5)
      'id' => int 1
      'type' =>
        object(FieldtypePageTitle)[133]
          private 'allowTextFormatters' (FieldtypeText) => boolean true
          protected 'data' =>
            array (size=0)
              empty
          protected 'useFuel' => boolean true
          private 'className' (Wire) => string 'FieldtypePageTitle' (length=18)
          protected 'localHooks' =>
            array (size=0)
              empty
          protected 'trackChanges' => boolean false
          private 'changes' (Wire) =>
            array (size=0)
              empty
      'flags' => int 13
      'name' => string 'title' (length=5)
      'label' => string 'Title' (length=5)
  protected 'prevTable' => null
  protected 'prevFieldtype' => null
  protected 'data' =>
    array (size=4)
      'required' => int 1
      'textformatters' =>
        array (size=1)
          0 => string 'TextformatterEntities' (length=21)
      'size' => int 0
      'maxlength' => int 255
  protected 'useFuel' => boolean true
  private 'className' (Wire) => string '' (length=0)
  protected 'localHooks' =>
    array (size=0)
      empty
  protected 'trackChanges' => boolean true
  private 'changes' (Wire) =>
    array (size=0)
      empty

(string) $field; see type casting -> www.php.net/manual/en/language.types.type-juggling.php

  • Like 2
Link to comment
Share on other sites

So... after reading this, assuming my field is: myfield

$fields->get("myfield");

$page->template->fields->myfield;

$page->get("myfield");

$page->myfield;

all basically get the same result?

for example: (string) $field; // is not clear to me, must I use <php? (string) $field; ?>

And yes, all are right, I need to go through those docs, but how to match what you learn to understand the cheatsheet?

I know what a string, variable, array is. But an object???

Link to comment
Share on other sites

$fields->get("myfield"); // (object) derived from Field object,


$page->template->fields->myfield; (object) derived from Field object, 


$page->get("myfield"); // (string) value entered in the backed for that page


$page->myfield; // (string) value entered in the backed for that page


  • Like 1
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...