Example #1
0
/**
 * mm_renameField
 * @version 1.2 (2013-05-16)
 *
 * Change the label for an element.
 * 
 * @uses ManagerManager plugin 0.4.
 * 
 * @param fields {comma separated string} - The name(s) of the document fields (or TVs) this should apply to. @required
 * @param newlabel {string} - The new text for the label. @required
 * @param roles {comma separated string} - The roles that the widget is applied to (when this parameter is empty then widget is applied to the all roles).
 * @param templates {comma separated string} - Id of the templates to which this widget is applied (when this parameter is empty then widget is applied to the all templates).
 * @param newhelp {string} - New text for the help icon with this field or for comment with TV. The same restriction apply as when using mm_changeFieldHelp directly.
 * 
 * @link http://code.divandesign.biz/modx/mm_renamefield/1.2
 * 
 * @copyright 2013
 */
function mm_renameField($fields, $newlabel, $roles = '', $templates = '', $newhelp = '')
{
    global $mm_fields, $modx;
    $e =& $modx->Event;
    // if the current page is being edited by someone in the list of roles, and uses a template in the list of templates
    if ($e->name == 'OnDocFormRender' && useThisRule($roles, $templates)) {
        $fields = makeArray($fields);
        if (count($fields) == 0) {
            return;
        }
        $output = "//  -------------- mm_renameField :: Begin ------------- \n";
        foreach ($fields as $field) {
            $element = '';
            switch ($field) {
                // Exceptions
                case 'keywords':
                    $element = '$j("select[name*=keywords]").siblings("span.warning")';
                    break;
                case 'metatags':
                    $element = '$j("select[name*=metatags]").siblings("span.warning")';
                    break;
                case 'hidemenu':
                case 'show_in_menu':
                    $element = '$j("input[name=\'hidemenucheck\']").siblings("span.warning")';
                    break;
                case 'which_editor':
                    $element = '$j("#which_editor").prev("span.warning")';
                    break;
                case 'content':
                    $element = '$j("#content_header")';
                    break;
                case 'menuindex':
                    $element = '$j("input[name=\'menuindex\']").parents("table:first").parents("td:first").prev("td").find("span.warning")';
                    break;
                    // Ones that follow the regular pattern
                // Ones that follow the regular pattern
                default:
                    if (isset($mm_fields[$field])) {
                        $fieldtype = $mm_fields[$field]['fieldtype'];
                        $fieldname = $mm_fields[$field]['fieldname'];
                        $element = '$j("' . $fieldtype . '[name=\'' . $fieldname . '\']").parents("td:first").prev("td").children("span.warning")';
                    }
                    break;
            }
            if ($element != '') {
                $output .= $element . '.contents().filter(function(){return this.nodeType === 3;}).replaceWith("' . jsSafe($newlabel) . '");';
            }
            // If new help has been supplied, do that too
            if ($newhelp != '') {
                mm_changeFieldHelp($field, $newhelp, $roles, $templates);
            }
        }
        $output .= "//  -------------- mm_renameField :: End ------------- \n";
        $e->output($output . "\n");
    }
}
/**
 * mm_renameField
 * @version 1.1 (2012-11-13)
 *
 * Change the label for an element.
 * 
 * @uses ManagerManager plugin 0.4.
 * 
 * @link http://code.divandesign.biz/modx/mm_renamefield/1.1
 * 
 * @copyright 2012
 */
function mm_renameField($field, $newlabel, $roles = '', $templates = '', $newhelp = '')
{
    global $mm_fields, $modx;
    $e =& $modx->event;
    // if the current page is being edited by someone in the list of roles, and uses a template in the list of templates
    if ($e->name == 'OnDocFormRender' && useThisRule($roles, $templates)) {
        $output = "//  -------------- mm_renameField :: Begin ------------- \n";
        switch ($field) {
            // Exceptions
            case 'keywords':
                $output .= '$j("select[name*=keywords]").siblings("span.warning").html("' . jsSafe($newlabel) . '");';
                break;
            case 'metatags':
                $output .= '$j("select[name*=metatags]").siblings("span.warning").html("' . jsSafe($newlabel) . '");';
                break;
            case 'hidemenu':
            case 'show_in_menu':
                $output .= '$j("input[name=hidemenucheck]").siblings("span.warning").html("' . jsSafe($newlabel) . '");';
                break;
            case 'which_editor':
                $output .= '$j("#which_editor").prev("span.warning").html("' . jsSafe($newlabel) . '");';
                break;
            case 'content':
                $output .= '$j("#content_header").html("' . jsSafe($newlabel) . '")';
                break;
            case 'menuindex':
                $output .= '$j("input[name=menuindex]").parents().parents("td:first").prev("td").children("span.warning").empty().prepend("' . jsSafe($newlabel) . '");';
                break;
                // Ones that follow the regular pattern
            // Ones that follow the regular pattern
            default:
                if (isset($mm_fields[$field])) {
                    $fieldtype = $mm_fields[$field]['fieldtype'];
                    $fieldname = $mm_fields[$field]['fieldname'];
                    $output .= '$j("' . $fieldtype . '[name=' . $fieldname . ']").parents("td:first").prev("td").children("span.warning").empty().prepend("' . jsSafe($newlabel) . '");';
                }
                break;
        }
        $output .= "//  -------------- mm_renameField :: End ------------- \n";
        $e->output($output . "\n");
        // If new help has been supplied, do that too
        if ($newhelp != '') {
            mm_changeFieldHelp($field, $newhelp, $roles, $templates);
        }
    }
}
mm_renameField('longtitle', 'Headline', $news_role, '', 'This will be displayed at the top of each page');
// We'd like to treat our news stories differently from other documents, so let's customise them. They use a specific
// template (ID 10) so let's set a variable with this ID in.
$news_tpl = '10';
// We categorise our news stories with a TV (news_category), so let's put this on a new tab to make it obvious to editors
mm_createTab('Categories', 'cats', '', $news_tpl, '', '600');
mm_moveFieldsToTab('news_category', 'cats', '', $news_tpl);
// Some of our field names could be clarified for news stories...
mm_changeFieldHelp('longtitle', 'The story\'s headline', '', $news_tpl);
mm_changeFieldHelp('introtext', 'A short summary of the story', '', $news_tpl);
// We don't need to show these, as news stories aren't shown in menus
mm_hideFields('menuindex,show_in_menu', '', $news_tpl);
// Always make the page, menu and long titles the same
mm_synch_fields('pagetitle,menutitle,longtitle', '', $news_tpl);
// Set some defaults for everyone
// Always set the default publication date to today
mm_default('pub_date');
// Change the introtext field name to something more plain English
mm_renameField('introtext', 'Summary');
// and do the same for some of the help messages
mm_changeFieldHelp('alias', 'The URL that will be used to reach this story. Only numbers, letters and hyphens can be used');
// Add some widgets to certain TVs
mm_widget_tags('blogTags');
// Give blog tag editing capabilities to the 'blogTags' TV
mm_widget_colors('colour', '#666666');
// make a color selector widget for the 'colour' TV
mm_widget_showimagetvs();
// Always give a preview of Image TVs
// For everyone except administrators - this field doesn't mean much to anyone else
mm_hideFields('link_attributes', '!1');
// ------------------------ END OF RULES --------------------