View source
<?php
interface FlexiformElementInterface {
}
class FlexiformElement implements FlexiformElementInterface {
protected $flexiform;
protected $element_namespace;
protected $entity_namespace;
protected $entity_type;
protected $bundle;
protected $weight;
protected $settings;
public function __construct($flexiform, $settings, $element_namespace = '') {
$this->flexiform = $flexiform;
$this->entity_namespace = $settings['entity_namespace'];
$this->entity_type = $flexiform->entities[$settings['entity_namespace']]['entity_type'];
$this->bundle = $flexiform->entities[$settings['entity_namespace']]['bundle'];
$this->element_name = $settings['element_name'];
$this->element_info = flexiform_get_element_info($this->entity_type, $this->bundle, $this->element_name);
$this->settings = $settings;
$this->weight = !empty($settings['weight']) ? $settings['weight'] : 0;
$this->settings += array(
'display_options' => array(),
);
$this->element_namespace = !empty($element_namespace) ? $element_namespace : $this
->makeElementNamespace();
}
public function label() {
if (!empty($this->settings['label'])) {
return $this->settings['label'];
}
return $this->element_info['label'];
}
public function setLabel($label) {
$this->settings['label'] = $label;
}
public function name() {
return $this->element_name;
}
public function type() {
return $this->element_info['type'];
}
public function getElementNamespace() {
return $this->element_namespace;
}
public function getEntityNamespace() {
return $this->entity_namespace;
}
public function getEntityType() {
return $this->entity_type;
}
public function getWeight() {
return $this->weight;
}
public function setWeight($weight) {
$this->weight = $weight;
}
public function getSettings() {
return $this->settings;
}
public function configureForm($form, &$form_state, $flexiform) {
$form['label'] = array(
'#type' => 'textfield',
'#title' => t('Label'),
'#default_value' => !empty($this->settings['label']) ? $this->settings['label'] : '',
'#weight' => -99,
);
$form['display_options'] = array(
'#tree' => TRUE,
'#type' => 'fieldset',
'#title' => t('Display Options'),
'#weight' => 50,
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$form['display_options']['css_classes'] = array(
'#type' => 'textfield',
'#title' => t('Additional CSS Classes'),
'#default_value' => !empty($this->settings['display_options']['css']) ? $this->settings['display_options']['css'] : '',
);
$form['display_options']['hide_label'] = array(
'#type' => 'checkbox',
'#title' => t('Hide the Element Label'),
'#default_value' => !empty($this->settings['display_options']['hide_label']),
);
$form['display_options']['hide_element'] = array(
'#type' => 'checkbox',
'#title' => t('Hide the Element'),
'#description' => t('Completely hide the element from the form. If hidden an element will pass through its default values.'),
'#default_value' => !empty($this->settings['display_options']['hide_element']),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save'),
'#weight' => 100,
);
return $form;
}
public function configureFormValidate($form, &$form_state, $flexiform) {
}
public function configureFormSubmit($form, &$form_state, $flexiform) {
if (isset($form_state['values']['label'])) {
$this->settings['label'] = $form_state['values']['label'];
}
else {
$this->settings['label'] = $form_state['values']['instance']['label'];
}
$this->settings['display_options']['css'] = $form_state['values']['display_options']['css_classes'];
$this->settings['display_options']['hide_label'] = $form_state['values']['display_options']['hide_label'];
$this->settings['display_options']['hide_element'] = $form_state['values']['display_options']['hide_element'];
$flexiform
->updateElement($this);
$flexiform
->save();
$form_state['redirect'] = 'admin/structure/flexiforms/manage/' . $flexiform->form . '/form-fields';
}
public function removeForm($form, &$form_state, $flexiform) {
$form['confirm'] = array(
'#type' => 'hidden',
'#value' => 1,
);
$question = t('Are you sure you want to remove @element?', array(
'@element' => $this
->label(),
));
drupal_set_title($question, PASS_THROUGH);
$form['#attributes']['class'][] = 'confirmation';
$form['description'] = array(
'#markup' => t('This action cannot be undone.'),
);
$form['actions'] = array(
'#type' => 'actions',
);
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => t('Confirm'),
);
$form['actions']['cancel'] = array(
'#type' => 'link',
'#title' => t('Cancel'),
'#href' => 'admin/structure/flexiforms/manage/' . $flexiform->form . '/form-fields',
);
$form['#theme'] = 'confirm_form';
return $form;
}
public function removeFormValidate($form, &$form_state, $flexiform) {
}
public function removeFormSubmit($form, &$form_state, $flexiform) {
$flexiform
->removeElement($this);
$flexiform
->save();
$form_state['redirect'] = 'admin/structure/flexiforms/manage/' . $flexiform->form . '/form-fields';
}
public function form($form, &$form_state, $entity) {
$form[$this->element_namespace]['#weight'] = $this
->getWeight();
$form[$this->element_namespace]['#flexiform'] = $this->flexiform;
$form[$this->element_namespace]['#flexiform_element'] = $this;
if (!empty($form[$this->element_namespace]['#title']) && !empty($this->settings['display_options']['hide_label'])) {
$form[$this->element_namespace]['#title_display'] = 'invisible';
}
if (!empty($form[$this->element_namespace]['#type']) && !empty($this->settings['display_options']['hide_element'])) {
$form[$this->element_namespace]['#access'] = FALSE;
}
if (!empty($this->settings['display_options']['css'])) {
foreach (explode(' ', $this->settings['display_options']['css']) as $class) {
$form[$this->element_namespace]['#attributes']['class'][] = $class;
}
}
return $form;
}
public function formValidate($form, &$form_state, $entity) {
}
public function formSubmit($form, &$form_state, $entity) {
}
public function formExtractValues($form, &$form_state, $entity) {
return array();
}
public function formIsEmpty($form, &$form_state, $entity) {
$values = $this
->formExtractValues($form, $form_state, $entity);
return empty($values);
}
public function toSettingsArray() {
$settings = array(
'entity_namespace' => $this->entity_namespace,
'element_name' => $this->element_name,
'weight' => $this
->getWeight(),
'label' => $this
->label(),
) + $this->settings;
return $settings;
}
public function makeElementNamespace() {
return $this->entity_namespace . ':' . $this->element_name;
}
public function getCtoolsContexts($entities = array()) {
global $user;
ctools_include('context');
$contexts = array(
'global' => ctools_context_create('token'),
'current-user' => ctools_context_create('entity:user', $user),
);
$contexts['global']->keyword = 'global';
$contexts['current-user']->keyword = 'current-user';
$contexts['current-user']->identifier = t('Logged-in user');
foreach ($this->flexiform->entities as $namespace => $info) {
$type = 'entity:' . $info['entity_type'];
if (isset($entities[$namespace])) {
$contexts[$namespace] = ctools_context_create($type, $entities[$namespace]);
}
else {
$contexts[$namespace] = ctools_context_create_empty($type);
}
$contexts[$namespace]->keyword = $namespace;
$contexts[$namespace]->identifier = $info['label'];
}
return $contexts;
}
public function replaceCtoolsSubstitutions($string, $entities, $keywords = array()) {
$contexts = $this
->getCtoolsContexts($entities);
return ctools_context_keyword_substitute($string, $keywords, $contexts);
}
public function getCtoolsSubstitutionsList($keywords = array()) {
$content = array(
'#theme' => 'table',
'#header' => array(
t('Keyword'),
t('Value'),
),
'#rows' => array(),
);
foreach ($this
->getCtoolsContexts() as $context) {
foreach (ctools_context_get_converters('%' . check_plain($context->keyword) . ':', $context) as $keyword => $title) {
$content['#rows'][] = array(
check_plain($keyword),
t('@identifier: @title', array(
'@title' => $title,
'@identifier' => $context->identifier,
)),
);
}
}
if (count($content['#rows'])) {
return $content;
}
else {
return array();
}
}
public static function getElement($flexiform, $element_namespace) {
$cached_elements =& drupal_static(__CLASS__, array());
if (empty($cached_elements[$flexiform->form][$element_namespace])) {
$settings = $flexiform->elements[$element_namespace];
$entity_namespace = $settings['entity_namespace'];
$element_name = $settings['element_name'];
$entity_type = $flexiform->entities[$entity_namespace]['entity_type'];
$bundle = $flexiform->entities[$entity_namespace]['bundle'];
$element_info = flexiform_get_element_info($entity_type, $bundle, $element_name);
$class_name = $element_info['class'];
if (!class_exists($class_name) || !in_array('FlexiformElementInterface', class_implements($class_name))) {
watchdog('flexiform', 'Invalid Flexiform Element class !class_name for element !element_name', array(
'!class_name' => $class_name,
'!element_name' => $element_name,
), WATCHDOG_WARNING);
$class_name = 'FlexiformElementBroken';
}
$cached_elements[$flexiform->form][$element_namespace] = new $class_name($flexiform, $settings, $element_namespace);
}
return $cached_elements[$flexiform->form][$element_namespace];
}
public static function createElement($flexiform, $settings) {
$entity_namespace = $settings['entity_namespace'];
$element_name = $settings['element_name'];
$entity_type = $flexiform->entities[$entity_namespace]['entity_type'];
$bundle = $flexiform->entities[$entity_namespace]['bundle'];
$element_info = flexiform_get_element_info($entity_type, $bundle, $element_name);
$class_name = $element_info['class'];
if (!class_exists($class_name) || !in_array('FlexiformElementInterface', class_implements($class_name))) {
watchdog('flexiform', 'Invalid Flexiform Element class !class_name for element !element_name', array(
'!class_name' => $class_name,
'!element_name' => $element_name,
), WATCHDOG_WARNING);
$class_name = 'FlexiformElementBroken';
}
return new $class_name($flexiform, $settings);
}
}