# ghostkit/3.4.1/gutenberg/blocks/form/field-attributes/index.php

Block Animations, Motion &amp; Scroll Effects – Ghost Kit, version 3.4.1. 142 lines.

- Page: https://pluginprobe.com/plugins/ghostkit/3.4.1/code/gutenberg/blocks/form/field-attributes/index.php
- Raw: https://pluginprobe.com/plugins/ghostkit/3.4.1/raw/gutenberg/blocks/form/field-attributes/index.php
- Modified: 2024-02-18T18:40:24+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/ghostkit/3.4.1/code/gutenberg/blocks/form/field-attributes/index.php#L10-L20`.

```php
<?php
/**
 * Form field attributes.
 *
 * @package ghostkit
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Class GhostKit_Form_Field_Attributes
 */
class GhostKit_Form_Field_Attributes {
	/**
	 * Prepare attributes array for registering block.
	 *
	 * @param array $attributes array with custom attributes.
	 */
	public static function get_block_attributes( $attributes ) {
		$default_attributes = array(
			'slug' => array(
				'type' => 'string',
			),

			'label' => array(
				'type'    => 'string',
				'default' => esc_html__( 'Field Label', 'ghostkit' ),
			),
			'description' => array(
				'type' => 'string',
			),
			'descriptionHidden' => array(
				'type' => 'boolean',
			),
			'placeholder' => array(
				'type' => 'string',
			),
			'default' => array(
				'type' => 'string',
			),
			'hideLabel' => array(
				'type' => 'boolean',
			),
			'hideDescription' => array(
				'type' => 'boolean',
			),
			'required' => array(
				'type' => 'boolean',
			),

			'className' => array(
				'type' => 'string',
			),
		);

		if ( ! empty( $default_attributes ) ) {
			foreach ( $default_attributes as $k => $val ) {
				if ( isset( $attributes[ $k ] ) ) {
					$attributes[ $k ] = array_merge(
						$val,
						$attributes[ $k ]
					);
				} else {
					$attributes[ $k ] = $val;
				}
			}
		}

		return $attributes;
	}

	/**
	 * Prepare attributes string for fields.
	 *
	 * @param array $attributes attributes list.
	 */
	public static function get( $attributes ) {
		$allowed_attributes = array(
			'id',
			'placeholder',
			'default',
			'min',
			'max',
			'step',
			'multiple',
			'required',
			'for',
			'name',
		);

		if ( isset( $attributes['slug'] ) ) {
			$attributes['name'] = $attributes['slug'];
		}

		if ( isset( $attributes['slug'] ) && ! isset( $attributes['id'] ) ) {
			$attributes['id'] = $attributes['slug'];
		}

		if ( $attributes && ! empty( $attributes ) ) {
			foreach ( $attributes as $k => $val ) {
				if ( ! empty( $val ) && '' !== $val ) {
					if ( ! in_array( $k, $allowed_attributes, true ) ) {
						continue;
					}

					// boolean value.
					if ( is_bool( $val ) ) {
						if ( $val ) {
							$val = $k;
						} else {
							continue;
						}
					}

					// default attribute.
					if ( 'default' === $k ) {
						$k = 'value';
					}

					// value attribute.
					if ( 'name' === $k ) {
						if ( isset( $attributes['slug_sub'] ) ) {
							$val .= '[' . $attributes['slug_sub'] . ']';
						} else {
							$val .= '[value]';
						}

						if ( isset( $attributes['fieldIsArray'] ) && $attributes['fieldIsArray'] ) {
							$val .= '[]';
						}
					}

					echo ' ' . esc_attr( $k ) . '="' . esc_attr( $val ) . '"';
				}
			}
		}
	}
}
new GhostKit_Form_Field_Attributes();

```
