Last active
October 7, 2016 12:55
-
-
Save cifren/4db925f5abbcd64d05da3c3017e073bc to your computer and use it in GitHub Desktop.
Create a custom block with a form using ECK
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
include_once('ProdAvailabilityRepository.php'); | |
function custom_module_block_info() { | |
$blocks['product_availability'] = array( | |
// info: The name of the block. | |
'info' => t('Product availability form'), | |
); | |
return $blocks; | |
} | |
function custom_module_block_view($delta = '') { | |
// The $delta parameter tells us which block is being requested. | |
switch ($delta) { | |
case 'product_availability': | |
// Create your block content here | |
$block['subject'] = t('Title of first block (example_configurable_text)'); | |
// get repository object | |
$prodAvaRepository = new ProductAvailabilityRepository(); | |
// getItemIdFromProdAndRelay, get an id | |
if ($avai = $prodAvaRepository->getItemIdFromProdAndRelay(arg(1), arg(3))) { // if entity exist, edit mode | |
// load the entity using the ID | |
$entity = entity_load_single('product_availability', $avai); | |
$form_id = "eck__entity__form_edit_product_availability_product_availability"; | |
} else { // if entity doesn't exist, add mode | |
$options = array('type' => 'product_availability'); | |
// create an empty entity | |
$entity = entity_create('product_availability', $options); | |
$form_id = "eck__entity__form_add_product_availability_product_availability"; | |
} | |
// create form_state needed by form | |
$form_state = array( | |
'want form' => TRUE, | |
'build_info' => array( | |
'args' => array($entity) | |
) | |
); | |
// Use module_load_include so that caches and stuff can know to load this. | |
form_load_include($form_state, 'inc', 'eck', 'eck.entity'); | |
$form = drupal_build_form($form_id, $form_state); | |
$block['content'] = $form; | |
break; | |
} | |
return $block; | |
} | |
// ALTER FORM | |
function custom_module_form_eck__entity__form_add_product_availability_product_availability_alter($form, $form_state) | |
{ | |
custom_module_form_eck__entity__form_product_availability_product_availability_alter($form, $form_state); | |
} | |
function custom_module_form_eck__entity__form_edit_product_availability_product_availability_alter(&$form, &$form_state) | |
{ | |
custom_module_form_eck__entity__form_product_availability_product_availability_alter($form, $form_state); | |
} | |
function custom_module_form_eck__entity__form_product_availability_product_availability_alter(&$form, &$form_state) | |
{ | |
if (path_is_admin(current_path())) { | |
return; | |
} | |
// your content, like hidding entity reference field | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class ProductAvailabilityRepository { | |
static function getItemIdFromProdAndRelay($productId, $relayId) | |
{ | |
$dbQuery = db_query(" | |
SELECT eck_product_availability.id AS id | |
FROM {eck_product_availability} eck_product_availability | |
LEFT JOIN {field_data_field_relay_ref} field_data_field_relay_ref | |
ON eck_product_availability.id = field_data_field_relay_ref.entity_id AND field_data_field_relay_ref.entity_type = 'product_availability' | |
LEFT JOIN {field_data_field_product_ref} field_data_field_product_ref | |
ON eck_product_availability.id = field_data_field_product_ref.entity_id AND field_data_field_product_ref.entity_type = 'product_availability' | |
WHERE | |
(( (field_data_field_relay_ref.field_relay_ref_target_id = :relay_id ) | |
AND (field_data_field_product_ref.field_product_ref_target_id = :product_id ) )) | |
LIMIT 1 | |
", array( | |
':relay_id' => $relayId, | |
':product_id' => $productId | |
)); | |
$results = $dbQuery->fetchCol(); | |
return array_shift($results); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment