Skip to content

Commit

Permalink
Issue backdrop-contrib#18: Add image browser to re-use existing images
Browse files Browse the repository at this point in the history
  • Loading branch information
indigoxela committed Aug 30, 2022
1 parent eda72d3 commit 6e37521
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 24 deletions.
11 changes: 3 additions & 8 deletions seo_meta.admin.inc
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ function seo_meta_settings_form() {
'file_validate_extensions' => array('gif png jpg jpeg'),
'file_validate_size' => array(file_upload_max_size()),
),
'#browser_view' => 'image_library',
);
$form['open_graph']['meta_og_site_name'] = array(
'#type' => 'textfield',
Expand Down Expand Up @@ -284,14 +285,8 @@ function seo_meta_settings_form_submit($form, &$form_state) {
else {
seo_meta_record_update(TYPE_DEFAULTS, $langcode, $data);
}

if (!empty($form_state['values']['meta_og_image_fid'])) {
$file = file_load($form_state['values']['meta_og_image_fid']);
$file->status = FILE_STATUS_PERMANENT;
file_save($file);
file_usage_add($file, 'seo_meta', 'user', $user->uid);
}

seo_meta_form_submit_update_file_usage($form, $form_state);

backdrop_set_message(t('The configuration options have been saved.'));
}

Expand Down
57 changes: 41 additions & 16 deletions seo_meta.module
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ function seo_meta_form_alter(&$form, $form_state, $form_id) {
$use_keywords = $config->get('use_keywords');

$form['#validate'][] = 'seo_meta_form_validate';
$form['#submit'][] = 'seo_meta_form_submit_update_file_usage';
// edit existing node or add new
$nid = isset($form['nid']['#value']) ? $form['nid']['#value'] : NULL;

Expand Down Expand Up @@ -275,6 +276,7 @@ function seo_meta_form_alter(&$form, $form_state, $form_id) {
'file_validate_extensions' => array('gif png jpg jpeg'),
'file_validate_size' => array(file_upload_max_size()),
),
'#browser_view' => 'image_library',
);
$form['seo_meta']['holder']['open_graph']['meta_og_site_name'] = array(
'#type' => 'textfield',
Expand Down Expand Up @@ -368,6 +370,45 @@ function seo_meta_form_validate($form, &$form_state) {
}
}
}

/**
* Submission callback to record file usage.
*
* Does not work with the multi-step forms (front page, views, terms).
*/
function seo_meta_form_submit_update_file_usage($form, &$form_state) {
// The node form.
if (isset($form['seo_meta']['holder'])) {
$default = $form['seo_meta']['holder']['open_graph']['meta_og_image_fid']['#default_value'];
$fid = $form_state['values']['seo_meta']['open_graph']['meta_og_image_fid'];
}
// The default settings form.
else {
$default = $form['open_graph']['meta_og_image_fid']['#default_value'];
$fid = $form_state['values']['meta_og_image_fid'];
}
$entity = !empty($form['#entity']) ? $form['#entity'] : NULL;

if ($fid) {
if ($fid != $default && $file = file_load($fid)) {
if ($entity) {
file_usage_add($file, 'seo_meta', $entity->entityType(), $entity->id());
}
else {
file_usage_add($file, 'seo_meta', 'file', $file->fid);
}
}
}
elseif ($default && $file = file_load($default)) {
if ($entity) {
file_usage_delete($file, 'seo_meta', $entity->entityType(), $entity->id());
}
else {
file_usage_delete($file, 'seo_meta', 'file', $file->fid);
}
}
}

/**
* Helper for collect data from node
*
Expand Down Expand Up @@ -405,22 +446,6 @@ function seo_meta_get_node_data(Node $node) {
return $data;
}

/**
* Implements hook_node_presave().
*/
function seo_meta_node_presave(Node $node) {

if (isset($node->seo_meta['enabled']) && $node->seo_meta['enabled'] == 1) {
// save OG image as permanent file
if (!empty($node->seo_meta['open_graph']['meta_og_image_fid'])) {
$file = file_load($node->seo_meta['open_graph']['meta_og_image_fid']);
$file->status = FILE_STATUS_PERMANENT;
file_save($file);
file_usage_add($file, 'file', 'file', $file->fid);
}
}
}

/**
* Implements hook_node_insert().
*/
Expand Down

0 comments on commit 6e37521

Please sign in to comment.