<?php

function mkbh_backend_tweaks_init()
{
    module_load_include('inc', 'user', 'user.pages');
}

/**
 * Implements hook_field_widget_form_alter().
 */
function mkbh_backend_tweaks_field_widget_form_alter(&$element, &$form_state, $context)
{
    if ($context['field']['type'] == 'image') {
        foreach (element_children($element) as $delta) {
            $element[$delta]['#process'][] = 'mkbh_backend_tweaks_image_field_process_alt_title';
        }
    }
}

/**
 * Process all image fields Alt and Title fields.
 * Remove title and set corresponding placeholders.
 *
 * @param $element
 * @param $form_state
 * @param $form
 *
 * @return mixed
 */
function mkbh_backend_tweaks_image_field_process_alt_title($element, &$form_state, $form)
{
    if (isset($element['alt'])) {
        $element['alt']['#title_display'] = 'invisible';
        $element['alt']['#attributes']['placeholder'] = $element['alt']['#title'];
    }

    if (isset($element['title'])) {
        $element['title']['#title_display'] = 'invisible';
        $element['title']['#attributes']['placeholder'] = $element['title']['#title'];
    }

    return $element;
}

/**
 * Implements hook_ctools_plugin_directory().
 */
function mkbh_backend_tweaks_ctools_plugin_directory($owner, $plugin_type)
{
    if ($owner == 'ctools' && in_array($plugin_type, ['access'])) {
        return 'ctools/plugins/' . $plugin_type;
    }
}
