Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CRM-16479: Support for image styles #364

Merged
merged 3 commits into from
Oct 12, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 101 additions & 19 deletions modules/views/civicrm/civicrm_handler_field_contact_image.inc
Original file line number Diff line number Diff line change
Expand Up @@ -3,65 +3,112 @@
class civicrm_handler_field_contact_image extends views_handler_field {
function option_definition() {
$options = parent::option_definition();
$options['alt'] = array(
'default' => '',
$options['url_only'] = array(
'default' => FALSE,
'boolean' => TRUE,
);
$options['title'] = array(
'default' => '',
);
);
$options['alt'] = array(
'default' => '',
);
$directory = $this->get_civi_relative_upload_path();
if (module_exists('image') && $directory !== FALSE) {
$options['image_style'] = array(
'default' => '',
);
}
$options['width'] = array(
'default' => '',
);
$options['height'] = array(
'default' => '',
);
$options['url_only'] = array(
'default' => FALSE,
'boolean' => TRUE,
);

return $options;
}

function options_form(&$form, &$form_state) {
parent::options_form($form, $form_state);
parent::options_form($form, $form_state);
$directory = $this->get_civi_relative_upload_path();

$form['url_only'] = array(
'#title' => t('Output the image URL only'),
'#description' => t('This option stops the URL being wrapped in an html <code>img</code> tag and ignores the title, alt, width and height settings. This may be useful for rewriting the results.'),
'#type' => 'checkbox',
'#default_value' => $this->options['url_only'],
);

$form['title'] = array(
'#title' => t('Title attribute'),
'#description' => t('The text to use as value for the <code>img</code> tag <code>title</code> attribute.'),
'#type' => 'textfield',
'#default_value' => $this->options['title'],
'#dependency' => array(
'edit-options-url-only' => array(0),
),
);

$form['alt'] = array(
'#title' => t('Alt attribute'),
'#description' => t('The text to use as value for the <code>img</code> tag <code>alt</code> attribute.'),
'#type' => 'textfield',
'#default_value' => $this->options['alt'],
'#dependency' => array(
'edit-options-url-only' => array(0),
),
);

if (module_exists('image') && $directory !== FALSE) {
$styles = image_styles();
$style_options = array('' => t('None (original image)'));
foreach ($styles as $style) {
$style_options[$style['name']] = $style['name'];
}
$form['image_style'] = array(
'#title' => t('Image style'),
'#description' => t('Display the contact image using an image style'),
'#type' => 'select',
'#options' => $style_options,
'#default_value' => $this->options['image_style'],
'#dependency' => array(
'edit-options-url-only' => array(0),
),
);
}

$form['width'] = array(
'#title' => t('Width'),
'#description' => t('Resize the image in the browser to this width. If left empty, the width will scale proportionally with the height.'),
'#type' => 'textfield',
'#default_value' => $this->options['width'],
'#element_validate' => array('element_validate_integer_positive'),
'#dependency' => array(
'edit-options-url-only' => array(0),
),
);

$form['height'] = array(
'#title' => t('Height'),
'#description' => t('Resize the image in the browser to this height. If left empty, the height will scale proportionally with the width.'),
'#type' => 'textfield',
'#default_value' => $this->options['height'],
'#element_validate' => array('element_validate_integer_positive'),
'#dependency' => array(
'edit-options-url-only' => array(0),
),
);
$form['url_only'] = array(
'#title' => t('Output the image URL only'),
'#description' => t('This option stops the URL being wrapped in an html <code>img</code> tag and ignores the title, alt, width and height settings. This may be useful for rewriting the results.'),
'#type' => 'checkbox',
'#default_value' => $this->options['url_only'],
);

if (module_exists('image') && $directory !== FALSE) {
$form['width']['#dependency']['edit-options-image-style'] = array('');
$form['width']['#dependency_count'] = 2;
$form['height']['#dependency']['edit-options-image-style'] = array('');
$form['height']['#dependency_count'] = 2;
}
}

function render($values) {
$value = $this->get_value($values);

if (empty($value)) {
return;
}
Expand All @@ -70,11 +117,46 @@ class civicrm_handler_field_contact_image extends views_handler_field {
}
$image = array(
'path' => $value,
'width' => empty($this->options['width']) ? 'auto' : $this->options['width'],
'height' => empty($this->options['height']) ? 'auto' : $this->options['height'],
'title' => $this->options['title'],
'alt' => $this->options['alt'],
);
return theme('image', $image);
$directory = $this->get_civi_relative_upload_path();
if ($this->options['image_style'] && module_exists('image') && $directory !== FALSE) {
$image['style_name'] = empty($this->options['image_style']) ? '' : $this->options['image_style'];
$image_path = $directory . substr(strstr($value, 'photo='), 6);
$image['path'] = str_replace('\\', '/', $image_path);
return theme('image_style', $image);
}
else {
$image['width'] = empty($this->options['width']) ? 'auto' : $this->options['width'];
$image['height'] = empty($this->options['height']) ? 'auto' : $this->options['height'];
return theme('image', $image);
}
}

/**
* Get the CiviCRM upload directory relative to the Drupal public upload
* directory so that we can support image styles.
*
* Returns the relative path if it is supported, otherwise returns FALSE.
*
* @return bool|string
*/
function get_civi_relative_upload_path() {
civicrm_initialize();
$wrapper = file_stream_wrapper_get_instance_by_uri('public://');
$drupal_upload_path = $wrapper->getDirectoryPath();
$civi_upload_path = Civi::paths()->getUrl(Civi::settings()->get('customFileUploadDir'));
$relative_upload_path = explode($drupal_upload_path, $civi_upload_path);

// If we couldn't explode() then Civi's upload path is not in the public://
// directory, so we cannot support image styles.
if (count($relative_upload_path) != 2) {
return FALSE;
}

// Otherwise, return the relative path without the leading slash
return substr($relative_upload_path[1], 1);
}

}