-
Notifications
You must be signed in to change notification settings - Fork 64
Repeater row count and row index function. #429
Changes from 4 commits
d43aad2
2326b9d
5b50bef
432554e
6300f33
574db41
6e972b1
f4080fe
a1bb136
8d0b4f6
2b61b64
6c39603
3e81bf3
39fade9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,12 +24,13 @@ class Repeater extends Control_Abstract { | |
/** | ||
* Field variable type. | ||
* | ||
* The Repeater control is an array of objects, with each row being an object. | ||
* For example, a repeater with one row might be [ { 'example-text': 'Foo', 'example-image': 4232 } ]. | ||
* The Repeater control is an array of arrays, with each row being its own array. | ||
* For example, a repeater with two rows might be: | ||
* [ 'rows': [ 0: [ 'example-text': 'Foo', 'example-image': 42 ], 1: [ 'example-text': 'Bar', 'example-image': 32 ] ] ]. | ||
* | ||
* @var string | ||
*/ | ||
public $type = 'object'; | ||
public $type = 'array'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good thing to bring up. In PHP, this would be an There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah – makes much more sense for |
||
|
||
/** | ||
* Repeater constructor. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -150,6 +150,47 @@ function reset_block_rows( $name ) { | |
block_lab()->loop()->reset( $name ); | ||
} | ||
|
||
/** | ||
* Return the total amount of rows in a repeater. | ||
* | ||
* @param string $name The name of the repeater field. | ||
* @return int|bool The total amount of rows. False if the repeater isn't found. | ||
*/ | ||
function block_row_count( $name ) { | ||
global $block_lab_attributes; | ||
|
||
if ( ! isset( $block_lab_attributes[ $name ] ) ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This function looks good overall. Is this conditional necessary? It seems that if it's not set, the block below will return false: if ( ! isset( $block_lab_attributes[ $name ]['rows'] ) ) {
return false;
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's a really good point. The thinking here is:
Given this, do you still think it's worth only doing the single (second) condition? I'm more than happy to make that change. Just wanted to explain my approach. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure, it's kind of a nitpick 😄 My guess is that there's not much performance difference between the existing approach, and an approach with only one There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Addressed in 6300f33. |
||
return false; | ||
} | ||
|
||
if ( ! isset( $block_lab_attributes[ $name ]['rows'] ) ) { | ||
return false; | ||
} | ||
|
||
return count( $block_lab_attributes[ $name ]['rows'] ); | ||
} | ||
|
||
/** | ||
* Return the index of the current repeater row. | ||
* | ||
* Note: The index is zero-based, which means that the first row in a repeater has | ||
* an index of 0, the second row has an index of 1, and so on. | ||
* | ||
* @param string $name (Optional) The name of the repeater field. | ||
* @return int|bool The index of the row. False if the repeater isn't found. | ||
*/ | ||
function block_row_index( $name = '' ) { | ||
if ( '' === $name ) { | ||
$name = block_lab()->loop()->active; | ||
} | ||
|
||
if ( ! isset( block_lab()->loop()->loops[ $name ] ) ) { | ||
return false; | ||
} | ||
|
||
return block_lab()->loop()->loops[ $name ]; | ||
} | ||
|
||
/** | ||
* Return the value of a sub-field. | ||
* | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could the logic in this class be in the Repeater class, in a
validate()
method?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in a1bb136.