Field methods - Multiple fields support

Now I can create a new field method, like this:

$item->my_field()->my_field_method();

That means I can manipulate the output of the field my_field with the method my_field_method.

Benefits of multiple fields in one field method

Lets say I have one field called min_cost and another one max_cost with a currency “integer” in each field. Then I might want to combine them before output so when it comes out in the HTML it looks like this:

1.000-200 USD

Practical

I don’t know how it might look if this was developed. Just some suggestions…

$page->my_field('my_field2')->my_field_method();

$page->bundle( array( 'my_field1', 'my_field2' ) )->my_field_method();

$page->my_field1->my_field_method( 'my_field2' );

I think you might need to explain this a little further. Cause at the moment I am wondering why you wouldn’t just do:

<?php echo $page->field1() ?>-<?php echo $page->field2() ?> USD

What you say work perfectly if field1 and field2 always exist and if no number formatting should be done. What I mean is more complex scenarios where you need to manipulate the data with if statements, loops, str_replace or what it might be.

Input data scenarios (fields, one value per row)

1

field1: 200

----

field2: 1000

2

field1: 1000

3

field1: 200

4

(nothing here)

Result output of the above

Notice this:

  • All numbers are formatted
  • All numbers are checked if they exist or not
  • If one number is missing there should not be a dash in between. Because there are no between.
  • If both numbers are missing there might be good with a notice
  • if there are no numbers, USD should not be added.

Result 1

200-1.000 USD

Result 2

1.000 USD

Result 3

200 USD

Result 4

No currency added

This could be coded into the template or as a plugin. I made it a plugin but I don’t use the $page for it, but I would like to.

In this case, this syntax could actually work:

Creating the field method, something like this:

field::$methods['my_field_method'] = function($field, $secondfield = null) {
  
  $field1 = $field->value;
  if($secondfield) $field2 = $field->page->$secondfield();

  // your logic
  $output = …

  return $output;
};

Maybe?

Another solution with field methods

This time I combined the two fields, one phone_number and one phone_number_linked.

HTML template

In a foreach where each page is an $item I use this:

echo $item->phone_number()->to_phone_link( 'phone_number_linked', $item );

Field method

Because I are allowed to use the $item object as parameter I can send that and the $field_slug to be able to find the value from it.

field::$methods['to_phone_link'] = function( $field, $field_slug = null, $item ) {
	$before = '';
	$after = '';
	if( ! empty( $field_slug )
        && $item->$field_slug()->isNotEmpty() ) {
		$before = '<a href="tel:' . $item->$field_slug() . '">';
		$after = '</a>';
	}
	$field->value = $before . $field->value . $after;
	return $field;
};

@distantnative It seems like we are on the same track.

Update 1 - No $item object redquired

I just realized that I don’t have to send $item object to the method as it is included in the $field

field::$methods['to_phone_link'] = function( $field, $field_slug = null ) {
	$before = '';
	$after = '';
	if( ! empty( $field_slug ) && $field->page->$field_slug()->isNotEmpty() ) {
		$before = '<a href="tel:' . $field->page->$field_slug() . '">';
		$after = '</a>';
	}
	$field->value = $before . $field->value . $after;
	return $field;
};

Update 2 - Using Kirby brick method

A little nicer maybe…

field::$methods['to_phone_link'] = function( $field, $field_slug = null ) {
	$a = brick('a');
	if( ! empty( $field_slug ) && $field->page->$field_slug()->isNotEmpty() ) {
		$a->attr( 'href', 'tel:' . $field->page->$field_slug() );
                $field->value = $a->append( $field->value );
	}
	return $field->value;
};

I think a feature like this could also be implemented using a model for the template.

2 Likes

How? You mean as a snippet or a plugin method?

It guess @lukasbestle refers to page models https://github.com/getkirby/getkirby.com/pull/75/files