kreativan
5/16/2019 - 5:50 PM

Custom FieldtypeOptionsMultiple

Processwire custom field type options multiple

<?php
/**
 *  FieldtypeExtraFields
 *
 *  @author Ivan Milincic <kreativan@outlook.com>
 *  @copyright 2019 Kreativan
 *
 *
*/

class FieldtypeExtraFields extends FieldtypeMulti {

	public static $defaultOptionValues = array();

	public static function getModuleInfo() {
		return array(
		'title' => 'Classifieds Extra Fields',
		'version' => 100,
		'summary' => 'Select multiple extra fields'
		);
	}

	public function getInputfield(Page $page, Field $fields) {

		// Input field
		$inputfield = $this->modules->get('InputfieldCheckboxes');

		// Options Arr - we wills tore all extra fields in this array
		// value needs to be an integer, [1 => "one"]
		$options = [];

		// Valid field types that can be used
		$valid_types = [
			"FieldtypeText",
			"FieldtypeTextLanguage",
			"FieldtypePageTitle",
			"FieldtypePageTitleLanguage",
			"FieldtypeOptions",
			"FieldtypePage",
			"FieldtypeInteger",
		];

		// Get Extra fields from FieldsetPage based on item_type
		if($page->what_type && !empty($page->what_type)) {
			$extra_fields = $this->fields->get("extra_fields_{$page->what_type->name}");
		}

		//$extra_fields = $modules->get("Classifieds")->getExtraFields($page->template);

		// Loop trough extra fields ad add valid types to the options array
		// value needs to be an integer, [1 => "one"]
		if(!empty($extra_fields)) {
			foreach($extra_fields->repeaterFields as $f) {
				$field = $this->fields->get($f);
				$id = $field->id;
				$label = !empty($field->label) ? $field->label : $field->label->name;
				if(in_array($field->type, $valid_types)) $options[$id] = $label;
			}
		}

		// Add options to the field
		foreach($options as $value => $label) {

			$value = $this->sanitizer->fieldName($value);
			$label = !empty($label) ? $label : $value;

			$inputfield->addOption($value, $label);

		}

		return $inputfield;

	}

	public function getDatabaseSchema(Field $field) {
		$schema = parent::getDatabaseSchema($field);
		$schema['data'] = 'int unsigned NOT NULL';
		$schema['sort'] = 'int unsigned NOT NULL';
		$schema['keys']['primary'] = 'PRIMARY KEY (pages_id, sort)';
		return $schema;
	}

	public function sanitizeValue(Page $page, Field $field, $value) {
		return $value;
	}




}