Skip to content

Instantly share code, notes, and snippets.

@litzinger
Created August 2, 2019 17:10
Show Gist options
  • Save litzinger/af8ff956c4c8ff320292125e7e204000 to your computer and use it in GitHub Desktop.
Save litzinger/af8ff956c4c8ff320292125e7e204000 to your computer and use it in GitHub Desktop.
ExpressionEngine fieldtype abstract and interface for simplifying building fieldtypes
<?php
abstract class FieldAbstract extends EE_Fieldtype
{
/**
* The settings from the db that EE passes to the ft file
* @var array
*/
public $settings = [];
/**
* The name of the field that EE passes to the ft file
* @var string
*/
public $field_name;
/**
* The field number/id that EE passes to the ft file
* @var integer
*/
public $field_id;
/**
* The POST name used when saving the field's settings in the CP
* @var string
*/
protected $settingsKey = '';
protected $cache = array();
protected $cacheKey = __CLASS__;
protected $settingsCacheKey = '';
protected $defaultSettings = [];
protected $siteId = 1;
/**
* Constructor
*
* @access public
*/
public function __construct()
{
parent::__construct();
$this->siteId = ee()->config->item('site_id');
if ( !isset(ee()->session->cache[$this->cacheKey])) {
ee()->session->cache[$this->cacheKey] = [];
}
$this->cache =& ee()->session->cache[$this->cacheKey];
}
/**
* By default, make it available everywhere.
*
* @param $name
* @return bool
*/
public function accepts_content_type($name)
{
return ($name == 'channel' || $name == 'grid' || $name == 'blocks/1');
}
/**
* Normal Fieldtype Display
* @param $data
* @return string
*/
public function display_field($data)
{
return $this->renderField($data, $this->field_name, $this->field_id);
}
/**
* Matrix Cell Display
* @param $data
* @return string
*/
public function display_cell($data)
{
return $this->renderField($data, $this->cell_name, $this->field_id);
}
/**
* Low Variables Fieldtype Display
*
* @param $data
* @return int entry_id of selected URL
*/
public function display_var_field($data)
{
return $this->renderField($data, $this->field_name);
}
/**
* @param $data
* @return string
*/
public function grid_display_field($data)
{
return $this->renderField($data, $this->field_name, $this->field_id);
}
/**
* Display normal individual field settings
* @param array $settings
* @return null
*/
public function display_settings($settings = [])
{
$rows = $this->getFieldSettings($settings);
if (!is_array($rows)) {
return null;
}
foreach ($rows as $row) {
if (isset($row[0]) && isset($row[1])) {
ee()->table->add_row($row[0], $row[1]);
}
}
return null;
}
/**
* Display Matrix Cell Settings
* @param $settings
* @return array
*/
public function display_cell_settings($settings)
{
return $this->getFieldSettings($settings);
}
/**
* Display Low Variables Settings
* @param $settings
* @return array
*/
public function display_var_settings($settings)
{
return $this->getFieldSettings($settings);
}
/**
* @param $settings
* @return array
*/
public function grid_display_settings($settings)
{
return $this->getFieldSettings($settings, true);
}
/**
* Just need an empty method here since we are not doing anything special.
*/
public function grid_save_settings()
{
return;
}
/*
* Save individual field settings
*/
/**
* @param string $settings
* @return array
*/
public function save_settings($settings = '')
{
return [$this->settingsKey => ee()->input->post($this->settingsKey)];
}
/**
* Save Matrix Cell Settings
* @param $settings
* @return array
*/
public function save_cell_settings($settings)
{
return [$this->settingsKey => (isset($settings[$this->settingsKey]) ? $settings[$this->settingsKey] : [])];
}
/**
* Save Low Variables Settings
*
* @param $settings
* @return array|mixed
*/
public function save_var_settings($settings)
{
return $this->save_settings($settings);
}
/**
* Save Normal Data
*
* @param $data
* @return string
*/
public function save($data)
{
if (!is_array($data)) {
return $data;
}
$cleanData = array();
foreach ($data as $key => $value) {
$cleanData[] = $value;
}
return json_encode($cleanData);
}
/**
* Save Matrix Cell Data
*
* @param $data
* @return string
*/
public function save_cell($data)
{
return $this->save($data);
}
/**
* Save Low Variables Data
* @param $data
* @return string
*/
public function save_var($data)
{
return $this->save($data);
}
/**
* @param $name
* @return mixed|string
*/
public function getSetting($name) {
return isset($this->settings[$name]) ? $this->settings[$name] : '';
}
/**
* @param $settings
* @param bool $isGrid
* @return array
*/
public function getFieldSettings($settings = [], $isGrid = false)
{
return isset($settings[$this->settingsKey]) ? $settings[$this->settingsKey] : null;
}
/**
* @param $data - The saved value from the field
* @param $name
* @param bool $fieldId
* @return string
*/
public function renderField($data, $name, $fieldId = null)
{
return '';
}
/**
* Creates a generic settings row in Grid
*
* @param string Left hand label for the row
* @param string Content of the row
* @param bool Wide row?
* @return string
*/
public function gridSettingsRow($label, $content, $wide = false)
{
$class = $wide ? 'grid_col_setting_label_small_width' : 'grid_col_setting_label_fixed_width';
return form_label(lang($label), NULL,
['class' => $class]
).$content;
}
/**
* @param $label
* @param $content
* @param string $description
* @param string $class
* @return string
*/
public function formField($content, $label = null, $class = null, $description = null)
{
if ($description) {
$content = '<span class="">'. $description .'</span>' . $content;
}
if ($label) {
$content = form_label(lang($label)) . $content;
}
return '<div class="'. $class .'">'. $content .'</div>';
}
/**
* Settings are provided to us via EE when it loads the fieldtype.
* It injects the settings into the public $settings property on the class.
* It does this for each field loaded on the page, so we get a unique
* cache per field instance.
*/
public function prepareSettings()
{
if (isset($this->settings[$this->settingsKey]) && is_array($this->settings[$this->settingsKey])) {
$this->settings = array_merge($this->defaultSettings, $this->settings[$this->settingsKey]);
} elseif (isset($this->settings[$this->settingsKey])) {
$this->settings = $this->settings[$this->settingsKey];
} else {
$this->settings = $this->defaultSettings;
}
$this->settingsCacheKey = md5(serialize($this->settings));
}
}
<?php
interface FieldInterface
{
/**
* Handles display in the CP, regardless of the parent field type (Grid, Matrix etc).
*
* @param $data
* @param $fieldName
* @param null $fieldId
* @return string
*/
function renderField($data, $fieldName, $fieldId = null);
/**
* Normal Fieldtype Display
* @param $data
* @return string
*/
function display_field($data);
/**
* Matrix Cell Display
* @param $data
* @return string
*/
function display_cell($data);
/**
* Low Variables Fieldtype Display
*
* @param $data
* @return int entry_id of selected URL
*/
function display_var_field($data);
/**
* @param $data
* @return string
*/
function grid_display_field($data);
/**
* Display normal individual field settings
* @param $settings
* @return null
*/
function display_settings($settings);
/**
* Display Matrix Cell Settings
* @param $settings
* @return array
*/
function display_cell_settings($settings);
/**
* Display Low Variables Settings
* @param $settings
* @return array
*/
function display_var_settings($settings);
/**
* @param $settings
* @return array
*/
function grid_display_settings($settings);
/**
* Just need an empty method here since we are not doing anything special.
*/
function grid_save_settings();
/**
* Save normal field settings
* @param string $settings
* @return array
*/
function save_settings($settings = '');
/**
* Save Matrix Cell Settings
* @param $settings
* @return array
*/
function save_cell_settings($settings);
/**
* Save Low Variables Settings
*
* @param $settings
* @return array|mixed
*/
function save_var_settings($settings);
/**
* Save Normal Data
*
* @param $data
* @return string
*/
function save($data);
/**
* Save Matrix Cell Data
*
* @param $data
* @return string
*/
function save_cell($data);
/**
* Save Low Variables Data
* @param $data
* @return string
*/
function save_var($data);
/**
* @param $name
* @return mixed|string
*/
function getSetting($name);
/**
* @param array $settings
* @param bool $isGrid
* @return array
*/
function getFieldSettings($settings = [], $isGrid = false);
/**
* @param $name
* @return bool
*/
function accepts_content_type($name);
/**
* Creates a generic settings row in Grid
*
* @param string Left hand label for the row
* @param string Content of the row
* @param bool Wide row?
* @return string
*/
function gridSettingsRow($label, $content, $wide = false);
/**
* @param $content
* @param $label
* @param string $description
* @param string $class
* @return string
*/
function formField($content, $label, $class = '', $description = '');
/**
* Settings are provided to us via EE when it loads the fieldtype.
* It injects the settings into the public $settings property on the class.
* It does this for each field loaded on the page, so we get a unique
* cache per field instance.
*/
function prepareSettings();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment