*/ public $parsed_block = []; /** * Whether the form block's own callback already used this context. * * @var bool */ public $used = false; /** * Field blocks rendered so far. * * @var int */ private $cursor = 0; /** * Build a context. * * @param string $form_id Effective formId. * @param string $stored_id formId as stored. * @param string $source Source reference. * @param int $instance Instance on the page. * @param Form_Schema $schema Schema of the form. * @param array $parsed Parsed form block. */ public function __construct( $form_id, $stored_id, $source, $instance, Form_Schema $schema, array $parsed ) { $this->form_id = (string) $form_id; $this->stored_id = (string) $stored_id; $this->source = (string) $source; $this->instance = max( 1, (int) $instance ); $this->schema = $schema; $this->parsed_block = $parsed; } /** * Resolved form attribute. * * @param string $name Attribute name. * @param mixed $default Fallback. * @return mixed */ public function attribute( $name, $default = null ) { return array_key_exists( $name, $this->schema->attributes ) ? $this->schema->attributes[ $name ] : $default; } /** * Label position: top, inline or hidden. * * @return string */ public function label_position() { return (string) $this->attribute( 'labelPosition', 'top' ); } /** * Required indicator: asterisk, text or none. * * @return string */ public function required_indicator() { return (string) $this->attribute( 'requiredIndicator', 'asterisk' ); } /** * The schema field for the next field block that renders. * * Falls back to resolving the block's own attributes when the schema has no * matching field (a field block that rendered outside the walk the resolver does). * * @param string $type Field type of the block that is rendering. * @param array $stored Stored attributes of that block. * @return array Field array: "type", every attribute at its resolved value, final "name". */ public function next_field( $type, array $stored ) { $index = $this->cursor; ++$this->cursor; if ( isset( $this->schema->fields[ $index ] ) && ( $this->schema->fields[ $index ]['type'] ?? '' ) === $type ) { return $this->schema->fields[ $index ]; } $field = array_merge( [ 'type' => $type ], Field_Types::resolve_field( $type, $stored ) ); if ( isset( $field['options'] ) && is_array( $field['options'] ) ) { $field['options'] = Schema_Resolver::normalise_options( $field['options'] ); } if ( ! Field_Types::is_valid_name( $field['name'] ) ) { $field['name'] = Schema_Resolver::derive_name( $field['name'], $type ); } return $field; } /** * Element ID (C-7.3): fw-{first 8 of the effective formId}-{name}-{instance}, plus an optional suffix. * * @param string $name Field name. * @param string $suffix Optional suffix such as "help", "error" or an option number. * @return string */ public function element_id( $name, $suffix = '' ) { $id = 'fw-' . substr( $this->form_id, 0, 8 ) . '-' . $name . '-' . $this->instance; return '' === $suffix ? $id : $id . '-' . $suffix; } /** * Form_State of this form instance. * * @return array */ public function state() { return Form_State::get( $this->form_id, $this->instance ); } }