View source
<?php
interface FlexiformBuilderInterface {
public function form($form, &$form_state);
public function formValidate($form, &$form_state);
public function formSubmit($form, &$form_state);
public function supportsComponent($component);
}
class FlexiformBuilder implements FlexiformBuilderInterface {
protected $flexiform;
protected $base_entity;
protected $base_entity_type;
public function __construct($flexiform, $base_entity) {
$this->flexiform = $flexiform;
$this->base_entity = $base_entity;
$this->base_entity_type = $flexiform->base_entity;
}
public function form($form, &$form_state) {
$form['#flexiform'] = $this->flexiform;
$form['#flexiform_base_entity'] = $this->base_entity;
$form['#flexiform_builder'] = $this;
$form['#theme'] = array(
'flexiform__' . $this->flexiform->form,
'flexiform',
);
$form['#contextual_links']['flexiform'] = array(
'admin/structure/flexiforms/manage',
array(
$this->flexiform->form,
),
);
$form['#attributes']['class'][] = 'flexiform--' . str_replace('_', '-', $this->flexiform->form);
return $form;
}
public function formValidate($form, &$form_state) {
}
public function formSubmit($form, &$form_state) {
}
public function formSubmitRedirect($form, &$form_state) {
if (empty($this->flexiform->settings['redirect']['path'])) {
return;
}
$redirect = $this->flexiform->settings['redirect']['path'];
$form_state['redirect'] = $this
->replaceCtoolsSubstitutions($redirect);
}
public function invoke(&$form, &$form_state, $hook = '') {
if (!empty($hook)) {
$hooks = array(
'flexiform_build_' . $hook,
'flexiform_build_' . $this->flexiform->builder . '_' . $hook,
);
}
else {
$hooks = array(
'flexiform_build',
'flexiform_build_' . $this->flexiform->builder,
);
}
drupal_alter($hooks, $form, $form_state, $this->flexiform);
}
public function getFlexiform() {
return $this->flexiform;
}
public function getFormEntity($namespace, &$state = array()) {
return $namespace == 'base_entity' ? $this->base_entity : FALSE;
}
public function supportsComponent($component) {
return FALSE;
}
public function getCtoolsContexts() {
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) {
try {
$entity = $this
->getFormEntity($namespace);
} catch (Exception $e) {
}
$type = 'entity:' . $info['entity_type'];
if (!empty($entity)) {
$contexts[$namespace] = ctools_context_create($type, $entity);
}
else {
$contexts[$namespace] = ctools_context_create_empty($type);
}
$contexts[$namespace]->keyword = $namespace;
$contexts[$namespace]->identifier = $info['label'];
}
return $contexts;
}
public function replaceCtoolsSubstitutions($string, $keywords = array()) {
$contexts = $this
->getCtoolsContexts();
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();
}
}
}