Last active
August 29, 2015 14:02
-
-
Save petercossey/d2a898b03f710448b8a7 to your computer and use it in GitHub Desktop.
A panels content plugin for displaying some text and an image.
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 | |
/** | |
* @file | |
* Welcome text and search form content plugin. | |
*/ | |
$plugin = array( | |
'title' => t('Welcome text search form'), | |
'single' => TRUE, | |
'category' => array(t('Content widgets'), -9), | |
'render callback' => 'welcome_text_search_form_render', | |
'edit form' => 'welcome_text_search_form_edit_form', | |
); | |
/** | |
* Render callback for the custom content type | |
*/ | |
function welcome_text_search_form_render($subtype, $conf, $args, $context) { | |
$block = new stdClass; | |
$block->title = NULL; | |
$output = ''; | |
$image = file_load($conf['image']); | |
$image_uri = (!empty($image)) ? file_create_url($image->uri) : ''; | |
$form = drupal_get_form('search_block_form'); | |
// Compose HTML markup | |
$output = '<div class="welcome-text-search-form" style="background-image:url(\'' . $image_uri . '\');">'; | |
$output .= '<div class="highlight-box"><p class="blurb">' . $conf['text'] . '</p>'; | |
$output .= render($form); | |
$output .= '</div></div>'; | |
$block->content = $output; | |
return $block; | |
} | |
/** | |
* 'Edit' callback for the content type. | |
*/ | |
function welcome_text_search_form_edit_form($form, &$form_state) { | |
$conf = $form_state['conf']; | |
// I stole this line from fieldable panel panes.. seems like we need it for | |
// the managed file form element. | |
ctools_form_include_file($form_state, $form_state['plugin']['path'] . '/' . $form_state['plugin']['file']); | |
$form['text'] = array( | |
'#type' => 'textfield', | |
'#title' => t('Welcome text'), | |
'#size' => 60, | |
'#description' => t('Text that is displayed with the search form'), | |
'#default_value' => !empty($conf['text']) ? $conf['text'] : '', | |
'#prefix' => '<div class="clear-block no-float">', | |
'#suffix' => '</div>', | |
); | |
$form['image'] = array( | |
'#title' => t('Image'), | |
'#type' => 'managed_file', | |
'#description' => t('The uploaded image will be displayed as the background for the content.'), | |
'#default_value' => !empty($conf['image']) ? $conf['image'] : '', | |
'#upload_location' => 'public://content_widgets/', | |
); | |
// If there is already an uploaded image display the image here. | |
$image_fid = (!empty($conf['image'])) ? $conf['image'] : NULL; | |
if ($image_fid) { | |
$image = file_load($image_fid); | |
$form['image_preview'] = array( | |
'#markup' => theme('image_style', array('style_name' => 'thumbnail', 'path' => $image->uri)), | |
); | |
} | |
return $form; | |
} | |
function welcome_text_search_form_edit_form_submit($form, &$form_state) { | |
// Save text | |
if (!empty($form_state['values']['text'])) { | |
$form_state['conf']['text'] = $form_state['values']['text']; | |
} | |
// Save image | |
if ($form_state['values']['image'] != 0) { | |
// The new file's status is set to 0 or temporary and in order to ensure | |
// that the file is not removed after 6 hours we need to change it's status | |
// to 1. Save the ID of the uploaded image for later use. | |
$file = file_load($form_state['values']['image']); | |
$file->status = FILE_STATUS_PERMANENT; | |
file_save($file); | |
// Manage usage count | |
file_usage_add($file, 'library_content_widgets', 'welcome_text_search_form', 1); | |
$form_state['conf']['image'] = $form_state['values']['image']; | |
} elseif ($form_state['values']['image'] == 0) { | |
// Retrieve the old file's id. | |
$fid = ($form_state['conf']['image']) ? $form_state['conf']['image'] : NULL; | |
$file = $fid ? file_load($fid) : FALSE; | |
if ($file) { | |
// When a module is managing a file, it must manage the usage count. | |
// Here we decrement the usage count with file_usage_delete(). | |
file_usage_delete($file, 'library_content_widgets', 'welcome_text_search_form', 1); | |
// The file_delete() function takes a file object and checks to see if | |
// the file is being used by any other modules. If it is the delete | |
// operation is cancelled, otherwise the file is deleted. | |
file_delete($file); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment