Italian (Italy)
Knowledge Base  >  Vik Restaurants  >  Hooks  >  Campi  >  Load Field

apply_filters( 'vikrestaurants_load_custom_field', string $classname, string $type )

Fires while creating a new instance for the provided custom field type.


Description

Trigger hook to allow external plugins to include new types of custom fields that might have been implemented out of this project.

Plugins must include here the file holding the class of the field type. The resulting class must inherit the E4J\VikRestaurants\CustomFields\Field declaration, otherwise it will be ignored.


Parameters

$classname

(string)  A custom classname that should be used to handle a specific type of custom fields.

$type

(string)  The custom field type.


Example

The example below adds support for a new "Radio Buttons" input type handler.

/**
 * Trigger hook to allow external plugins to include new types of custom fields
 * Fields that might have been implemented out of this project. Plugins must
 * include here the file holding the class of the field type.
 *
 * @param  string  $classname  The classname used to load the custom field type instance.
 * @param  string  $type       The requested custom field type.
 */
add_filter('vikrestaurants_load_custom_field', function($classname, $type) {
    if ($type === 'radio') {
        require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'types' . DIRECTORY_SEPARATOR . 'radio.php';
        $classname = 'RadioTypeCustomField';
    }

    return $classname;
}, 10, 2);

The class (shell) of the custom field will be built as follows.

use E4J\VikRestaurants\CustomFields\Field;

class RadioTypeCustomField extends Field
{
    /**
     * Returns the name of the field type.
     *
     * @return  string
     */
    public function getType()
    {
        return 'Radio Buttons';
    }

    /**
     * Extracts the value of the custom field and applies any
     * sanitizing according to the settings of the field.
     *
     * @return  mixed  A scalar value of the custom field.
     */
    protected function extract()
    {
        // extract through parent first
        $value = parent::extract($args);

        /**
         * @todo do stuff here (if needed)
         */

        return $value;
    }

    /**
     * Validates the field value.
     *
     * @param   mixed  $value  The field raw value.
     *
     * @return  bool   True if valid, false otherwise.
     */
    protected function validate($value)
    {
        // validate through parent first
        if (!parent::validate($value)) {
            return false;
        }

        // make sure the provided value is supported
        if (!in_array($value, $this->getOptions())) {
            return false;
        }

        return true;
    }

    /**
     * Returns the HTML of the field input.
     *
     * @param   array   $data  An array of display data.
     *
     * @return  string  The HTML of the input.
     */
    protected function getInput($data)
    {
        $html = '';

        // iterate all options
        foreach ($data['options'] as $k => $option) {
            // register radio button
            $html .= sprintf(
                '<input type="radio" name="%s" id="%s" class="%s" value="%s" %s style="width: auto !important; margin: 0;" />',
                $data['name'],
                $data['id'] . '-' . $k,
                $data['class'],
                $option,
                $option == $data['value'] ? 'checked' : ''
            );

            // register label
            $html .= sprintf(
                '<label for="%s" style="display: inline-block; margin: 0 12px 0 4px;">%s</label>',
                $data['id'] . '-' . $k,
                $option
            );
        }

        return $html;
    }
    
    /**
     * Returns an array of display data.
     *
     * @param   array   $data  An array of display data.
     *
     * @return  array
     */
    protected function getDisplayData(array $data)
    {
        // invoke parent first
        $data = parent::getDisplayData($data);

        // register available options within the display data
        $data['options'] = $this->getOptions();

        return $data;
    }

    /**
     * Helper method used to fetch the available options of the radio input.
     * 
     * @return  string[]
     */
    private function getOptions()
    {
        // assume the options are stored into a textarea, one per line
        $text = preg_split("/\R+/", $this->get('choose'));
        // get rid of empty values
        return array_values(array_filter($text));
    }
}

Changelog

Version Description
1.3 Introduced.
Ultimo aggiornamento: 2023-12-28
Utile?
Potrebbe interessarti:
This site uses cookies. By continuing to browse you accept their use. Further information