'Textarea',
'version' => 103,
'summary' => 'Field that stores multiple lines of text',
'permanent' => true,
);
}
public function init() {
$this->set('inputfieldClass', self::defaultInputfieldClass);
$this->set('contentType', self::contentTypeUnknown);
parent::init();
}
public function sanitizeValue(Page $page, Field $field, $value) {
return parent::sanitizeValue($page, $field, $value);
}
public function ___formatValue(Page $page, Field $field, $value) {
$value = parent::___formatValue($page, $field, $value);
return $value;
}
public function ___sleepValue(Page $page, Field $field, $value) {
$value = parent::___sleepValue($page, $field, $value);
if($field->contentType == self::contentTypeHTML) $this->htmlReplacements($value, true);
return $value;
}
public function ___wakeupValue(Page $page, Field $field, $value) {
// note: we do this here in addition to loadPageField to account for values that came
// from external resources (not loaded from DB).
$value = parent::___wakeupValue($page, $field, $value);
if($field->contentType == self::contentTypeHTML) $this->htmlReplacements($value, false);
return $value;
}
public function ___loadPageField(Page $page, Field $field) {
$value = parent::___loadPageField($page, $field);
if($field->contentType == self::contentTypeHTML) $this->htmlReplacements($value, false);
return $value;
}
/**
* Content Type HTML replacements accounting for href and src attributes
*
* This one handles a string value or array of string values (like for multi-language support)
*
* @param string|array $value Value to look for attributes (or array of values)
* @param bool $sleep When true, convert links starting with root URL to "/". When false, do the reverse.
*
*/
protected function htmlReplacements(&$value, $sleep = true) {
if(is_array($value)) {
foreach($value as $k => $v) {
$this->htmlReplacement($v, $sleep);
$value[$k] = $v;
}
} else if(is_string($value)) {
$this->htmlReplacement($value, $sleep);
}
}
/**
* Content Type HTML replacements accounting for href and src attributes
*
* This ensures that sites migrated from one subdirectory to another, or from a subdirectory to
* a non-subdir, or non-subdir to a subdir, continue working. This adds runtime context
* to 'href' and 'src' attributes in HTML.
*
* This method modifies the $value directly rather than returning it.
*
* In order to make the abstracted attributes identifiable to this function (so they can be reversed)
* it replaces the space preceding the attribute name with a tab character. This ensures the HTML
* underneath still remains compliant in case it is later extracted directly from the DB for
* data conversion or something like that.
*
* @param string $value Value to look for attributes
* @param bool $sleep When true, convert links starting with root URL to "/". When false, do the reverse.
*
*/
protected function htmlReplacement(&$value, $sleep = true) {
// see if quick exit possible
if(stripos($value, 'href=') === false && stripos($value, 'src=') === false) return;
$rootURL = $this->wire('config')->urls->root;
$replacements = array(
" href=\"$rootURL" => "\thref=\"/",
" href='$rootURL" => "\thref='/",
" src=\"$rootURL" => "\tsrc=\"/",
" src='$rootURL" => "\tsrc='/",
);
if($sleep) {
// sleep
$value = str_ireplace(array_keys($replacements), array_values($replacements), $value);
} else if(strpos($value, "\t") === false) {
// no wakeup necessary (quick exit)
return;
} else {
// wakeup
$value = str_ireplace(array_values($replacements), array_keys($replacements), $value);
}
}
public function getInputfield(Page $page, Field $field) {
if($field->inputfieldClass) {
$inputfield = $this->modules->getModule($field->inputfieldClass, array('noSubstitute' => true));
} else {
$inputfield = $this->modules->get(self::defaultInputfieldClass);
}
if(!$inputfield) {
$inputfield = $this->modules->get(self::defaultInputfieldClass);
$editURL = $this->wire('config')->urls->admin . "setup/field/edit?id=$field->id";
$modulesURL = $this->wire('config')->urls->admin . "module/";
$findURL = "http://modules.processwire.com/search/?q=$field->inputfieldClass";
$tab = '
';
$note = "
TO INSTALL:$tab 1. Go to Modules.$tab 2. click the \"New\" tab. $tab 3. For \"Module Class Name\" paste in \"$field->inputfieldClass\". $tab 4. Click \"Download & Install\".";
$note .= "
TO CHANGE: $tab 1. Edit the field. $tab 2. Click the \"Details\" tab. $tab 3. Select the \"Inputfield Type\". $tab 4. Click \"Save\".";
if($field->inputfieldClass == 'InputfieldTinyMCE') {
$this->wire('modules')->getInstall('InputfieldCKEditor'); // install it so it's ready for them
$this->error(
"Field '$field->name' uses TinyMCE, which is no longer part of the core. " .
"Please install TinyMCE " .
"or change it to use CKEditor (or another).$note",
Notice::allowMarkup);
} else if($field->inputfieldClass) {
$this->error(
"The module \"$field->inputfieldClass\" specified to provide input for field \"$field->name\" was not found. " .
"Please install $field->inputfieldClass " .
"or convert the field to use another input type.$note",
Notice::allowMarkup);
}
}
$inputfield->class = $this->className();
return $inputfield;
}
public function getDatabaseSchema(Field $field) {
$schema = parent::getDatabaseSchema($field);
$schema['data'] = 'mediumtext NOT NULL';
$schema['keys']['data'] = 'FULLTEXT KEY data (data)';
return $schema;
}
public function ___getConfigInputfields(Field $field) {
$inputfields = parent::___getConfigInputfields($field);
$f = $this->modules->get('InputfieldSelect');
$f->attr('name', 'inputfieldClass');
$f->attr('value', $field->inputfieldClass ? $field->inputfieldClass : self::defaultInputfieldClass);
$f->label = $this->_('Inputfield Type');
$f->description = $this->_('The type of field that will be used to collect input (Textarea is the default). Note that if you change this and submit, the available configuration options in the "input" tab section may change.'); // Inputfield type description
$f->required = true;
$baseClass = "InputfieldTextarea";
foreach($this->fuel('modules')->find("className^=Inputfield") as $fm) {
if("$fm" == $baseClass || is_subclass_of("$fm", $baseClass))
$f->addOption("$fm", str_replace("Inputfield", '', "$fm"));
}
$inputfields->append($f);
$f = $this->modules->get('InputfieldRadios');
$f->attr('name', 'contentType');
$f->label = $this->_('Content Type');
$f->addOption(self::contentTypeUnknown, $this->_('Unknown'));
$f->addOption(self::contentTypeHTML, $this->_('Markup/HTML'));
$f->attr('value', (int) $field->contentType);
$f->description = $this->_('The Markup/HTML option is recommended for fields using rich text editors (TinyMCE, CKEditor, etc.) and those containing HTML. It provides additional runtime filtering for quality assurance.'); // Content type description
$inputfields->append($f);
return $inputfields;
}
}