Skip to content

Instantly share code, notes, and snippets.

@craigrodway
Created November 17, 2013 17:11
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save craigrodway/7515600 to your computer and use it in GitHub Desktop.
Save craigrodway/7515600 to your computer and use it in GitHub Desktop.
Very simple Fieldtype that lists FormBuilder forms in a select box on editing pages.
<?php
/**
* ProcessWire Form Fieldtype.
* Stores reference to a single FormBuilder Form to allow easy selection on pages.
*
* ProcessWire 2.x
* Copyright (C) 2012 by Ryan Cramer
* Licensed under GNU/GPL v2, see LICENSE.TXT
*
* http://www.processwire.com
* http://www.ryancramer.com
*
*/
class FieldtypeForms extends Fieldtype {
public static function getModuleInfo()
{
return array(
'title' => 'FormBuilder Reference',
'version' => 100,
'summary' => 'Field that stores a reference to one FormBuilder form',
'permanent' => false,
);
}
public function getBlankValue(Page $page, Field $field)
{
return 0;
}
public function sanitizeValue(Page $page, Field $field, $value)
{
if ( ! $value) return 0;
$fb = $this->modules->get('FormBuilder');
return ($fb->isForm($value) ? (int) $value : 0);
}
public function ___wakeupValue(Page $page, Field $field, $value)
{
$v = $this->getBlankValue($page, $field);
return (empty($value) ? $v : $value);
}
public function ___sleepValue(Page $page, Field $field, $value)
{
return (is_int($value) ? $value : 0);
}
/**
* Instantiate the templates
*
* This method is only used when $page->outputFormatting is true.
*
*/
public function ___formatValue(Page $page, Field $field, $value)
{
if ( ! is_int($value)) return;
$fb = $this->modules->get('FormBuilder');
$form = $fb->load($value);
return ($form ? $form->name : NULL);
}
public function getInputfield(Page $page, Field $field)
{
$inputfield = $this->modules->get('InputfieldSelect');
$inputfield->attr('name', $field->name);
$fb = $this->modules->get('FormBuilder');
$forms = $fb->get('forms');
foreach ($forms as $id => $name)
{
$inputfield->addOption($id, $name);
}
return $inputfield;
}
public function getDatabaseSchema(Field $field)
{
$schema = parent::getDatabaseSchema($field);
$schema['data'] = 'int NOT NULL';
$schema['keys']['data'] = 'KEY data (data, pages_id)';
return $schema;
}
public function ___getCompatibleFieldtypes(Field $field)
{
$fieldtypes = new Fieldtypes();
return $fieldtypes;
}
}
@Da-Fecto
Copy link

Great, Thanks !

@Renobird
Copy link

Just about started building this exact thing the over the weekend. Thought I'd better search first (figured @somartist would have built something to be honest). Found a forum topic that led me here. Much appreciated.

@netcarver
Copy link

@craigrodway, any chance of publishing this in the PW modules repository?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment