Commit 90ceda06 authored by Natalia Gabrieleva's avatar Natalia Gabrieleva
Browse files

Initial commit of entityform block module

parents
Loading
Loading
Loading
Loading

entityform_block.info

0 → 100644
+6 −0
Original line number Diff line number Diff line
name = Entityforms Block
description = Allows to load a specified entity form as a block
dependencies[] = entityform
package = Entityforms
core = 7.x
files[] = entityform_block.module
+149 −0
Original line number Diff line number Diff line
<?php
/**
 * @file
 * Render any entity form into a block.
 */


/**
 * Implements hook_block_info().
 */
function entityform_block_block_info() {
  $blocks = array();
  $types = variable_get('entityform_block_types');
  if (!empty($types)) {
    foreach($types as $key => $type) {
      $blocks[$type] = array(
          'info' => $type,
          'cache' => DRUPAL_NO_CACHE,
      );
    }
  }
  return $blocks;
}

/**
 * Implements hook_block_view().
 */
function entityform_block_block_view($delta = '') {
  
  $entity_form = entityform_block_get_entity($delta);
  $subject = $entity_form[1]->label;
  $block = array();
   module_load_include('inc', 'entityform', 'entityform.admin');
  $block['subject'] = $subject;
  $block['content'] = entityform_form_wrapper(entityform_empty_load($delta));    
  return $block;
}

/**
 * Implements hook_form_alter().
 * 
 * Adds a checkbox to enable a block to the entity form edit form.
 */
function entityform_block_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'entityform_type_form') {
    $types = array();
    $types = variable_get('entityform_block_types');
    $current_type = $form['#entityform_type']->type;
    if (!empty($types) && isset($current_type)) {
      $exists = entityform_block_exists($types, $current_type);
      $form['data']['block_set']['current_type'] = array(
          '#type' => 'hidden',
          '#value' => $current_type,
      );
    }
    $form['data']['block_set'] = array(
        '#type' => 'fieldset',
        '#title' => t('Entity block settings'),
        '#collapsible' => TRUE,
        '#group' => 'additional_settings',
        '#weight' => 100,
    );
    $form['data']['block_set']['enable_block'] = array(
        '#type' => 'checkbox',
        '#title' => t('Enable as a block'),        
        '#description' => t('Create a block with this entity form?'),        
        '#default_value' => !empty($exists) ? 1 : 0, 
    );
    $form['#submit'][] = 'entityform_block_submit';
    
  }
  dpm($form);
}

/*
 * Custom submit function.
 */
function entityform_block_submit(&$form, &$form_state) {
  $types = array();
  $types = variable_get('entityform_block_types', $types);
  $original_type = isset($form_state['values']['data']['block_set']['current_type']) ? 
  $form_state['values']['data']['block_set']['current_type'] : '';
  $type = $form_state['values']['type'];
  if (!empty($types)) {
    $block_exists = entityform_block_exists($types, $type, $original_type); 
  }
  if ($form_state['values']['data']['block_set']['enable_block'] == 1) {
    $enabled = TRUE;
  }
  
  if (isset($enabled)) {
    // if the entityform block enabled but doesn't exist
    // add to the array
    if (!isset($block_exists)) {
      $types[] = $type;
    }
    // if the block exists, check if the entity form machine name has changed
    else {
      if ($original_type != $type) {
        $key = array_search($original_type, $types);
        $types[$key] = $type;
        entityform_block_change_name($original_type, $type);
      }
    } 
    // if it's not enabled but block exists
  } elseif (!isset($enabled) && $block_exists) { 
      $key = array_search($original_type, $types); 
      unset($types[$key]);
    }
  variable_set('entityform_block_types', $types);
  //dpm($form_state);
}

function entityform_block_exists($types = array(), $type, $original_type = '') {
  
  if (!empty($original_type) && $type !== $original_type) {
    $type_check = $original_type;
  } else {
    $type_check = $type;
  }
  if (in_array($type_check, $types)) {
    return TRUE;
  } else {
    return FALSE;
  }
}
/*
 * Update block name if the entity form's machine readable name has changed.
 * We're not using block deltas as they're not exportable.
 */
function entityform_block_change_name($original_type, $new_type) {
  $query = db_update('block')->fields(array('delta' => $new_type))
          ->condition('delta', $original_type)
          ->execute();
}
/*
 * Using entity field query to retrieve the entity object.
 */
function entityform_block_get_entity($type){
  $query = new EntityFieldQuery();
  $query->entityCondition('entity_type', 'entityform_type')
        ->propertyCondition('type', $type);
  $result = $query->execute();
  $id = $result['entityform_type'][1]->id;
  
  $entity_form = entity_load('entityform_type', array($id));
  return $entity_form;
}